@@ -58,55 +58,79 @@ Import-Module -Name pnp.powershell -DisableNameChecking
5858$CsvFilePath = "<PATH>\URLStoScan.csv"
5959$SiteUrls = Import-Csv -Path $CsvFilePath | Select-Object -ExpandProperty SiteUrl
6060
61- # Iterate through each SharePoint site URL
61+ # Define retention label mapping
62+ $RetentionLabelMapping = @{
63+ "Script Test 1 Old" = "Script Test 1 New"
64+ "Script Test 2 Old" = "Script Test 2 New"
65+ #"25 years" = "New 25 Years Label"
66+ #"30 years" = "New 30 Years Label"
67+ #"35 years" = "New 35 Years Label"
68+ }
69+
70+ # Function to process a single list item
71+ function Set-RetentionLabel {
72+ param (
73+ [Parameter(Mandatory)]
74+ $List,
75+
76+ [Parameter(Mandatory)]
77+ $Item,
78+
79+ [Parameter(Mandatory)]
80+ $FieldInternalName,
81+
82+ [Parameter(Mandatory)]
83+ $Mapping
84+ )
85+
86+ $CurrentLabel = $Item[$FieldInternalName]
87+ Write-Host "Item ID: $($Item.Id) | Current label: $CurrentLabel" -ForegroundColor Blue
88+
89+ if ($Mapping.ContainsKey($CurrentLabel)) {
90+ $NewLabel = $Mapping[$CurrentLabel]
91+ Set-PnPListItem -List $List -Identity $Item.Id -Label $NewLabel
92+ Write-Host "Updated label to: $NewLabel" -ForegroundColor Green
93+ } else {
94+ Write-Host "No matching retention label found for Item ID $($Item.Id) in List '$($List.Title)'" -ForegroundColor Yellow
95+ }
96+ }
97+
98+ # Iterate through each SharePoint site
6299foreach ($SiteUrl in $SiteUrls) {
63- Write-Host "Processing SharePoint site: $SiteUrl" -ForegroundColor yellow
64-
65- # Connect to SharePoint site
66- Connect-PnPOnline -Url $SiteUrl -interactive
67-
68- # Retrieve all lists and libraries
69- $Lists = Get-PnPList
70- Write-Host "Lists Retrieved!" -ForegroundColor Green
71- foreach ($List in $Lists) {
72- if ($List.BaseType -eq "GenericList" -or $List.BaseType -eq "DocumentLibrary") {
73- # Retrieve all items in the list or library
74- Write-Host "Retrieving List items for list" -ForegroundColor yellow
75- $Items = Get-PnPListItem -List $List
76-
77- # Set retention label field
78- $RetentionLabelField = Get-PnPField -List $List -Identity "_ComplianceTag"
79- write-host "Retention label field is: $RetentionLabelField"
80-
81- foreach ($Item in $Items) {
82- Write-Host "Processing Item" -ForegroundColor yellow
83- $ExistingRetentionLabel = $Item[$RetentionLabelField.InternalName]
84- Write-Host "Current label is $ExistingRetentionLabel" -ForegroundColor Blue
85-
86-
87- if ($ExistingRetentionLabel -eq "Script Test 1 Old") {
88- Set-PnPListItem -List $List -Identity $Item.Id -label "Script Test 1 New"
100+ Write-Host "Processing SharePoint site: $SiteUrl" -ForegroundColor Cyan
101+
102+ Connect-PnPOnline -Url $SiteUrl -Interactive
103+
104+ # Retrieve all lists and libraries
105+ $Lists = Get-PnPList | Where-Object { $_.BaseType -in @("GenericList","DocumentLibrary") }
106+ Write-Host "$($Lists.Count) lists/libraries retrieved." -ForegroundColor Green
107+
108+ foreach ($List in $Lists) {
109+ Write-Host "Processing list: $($List.Title)" -ForegroundColor Cyan
110+
111+ # Get retention label field
112+ $RetentionField = Get-PnPField -List $List -Identity "_ComplianceTag"
113+ if (-not $RetentionField) {
114+ Write-Host "Retention label field not found in list $($List.Title)." -ForegroundColor Red
115+ continue
89116 }
90- elseif ($ExistingRetentionLabel -eq "Script Test 2 Old") {
91- Set-PnPListItem -List $List -Identity $Item.Id -label "Script Test 2 New"
92- }
93- #elseif ($ExistingRetentionLabel -eq "25 years") {
94- # Set-PnPListItem -List $List -Identity $Item.Id -Values @{ "_ComplianceTag" = "New 25 Years Label" }
95- #}
96- #elseif ($ExistingRetentionLabel -eq "30 years") {
97- # Set-PnPListItem -List $List -Identity $Item.Id -Values @{ "_ComplianceTag" = "New 30 Years Label" }
98- #}
99- #elseif ($ExistingRetentionLabel -eq "35 years") {
100- # Set-PnPListItem -List $List -Identity $Item.Id -Values @{ "_ComplianceTag" = "New 35 Years Label" }
101- #}
102-
103- else {
104- Write-Host "No matching retention label found for item $($Item.Id) in $($List.Title)."
117+
118+ # Retrieve all items
119+ $Items = Get-PnPListItem -List $List
120+ Write-Host "$($Items.Count) items found in list $($List.Title)." -ForegroundColor Yellow
121+
122+ foreach ($Item in $Items) {
123+ Set-RetentionLabel -List $List -Item $Item -FieldInternalName $RetentionField.InternalName -Mapping $RetentionLabelMapping
105124 }
125+
126+ Write-Host "Finished processing list $($List.Title)." -ForegroundColor Green
106127 }
107-
108- Write-Host "Retention labels set successfully."
109- }}} stop-Transcript
128+
129+ Write-Host "Finished processing site $SiteUrl." -ForegroundColor Cyan
130+ }
131+
132+ Stop-Transcript
133+
110134```
111135[ !INCLUDE [ More about PnP PowerShell] ( ../../docfx/includes/MORE-PNPPS.md )]
112136
@@ -116,8 +140,9 @@ foreach ($SiteUrl in $SiteUrls) {
116140
117141| Author(s) |
118142| -----------|
119- | Nick Brattoli|
143+ | Nick Brattoli |
144+ | ojopiyo |
120145
121146
122147[ !INCLUDE [ DISCLAIMER] ( ../../docfx/includes/DISCLAIMER.md )]
123- <img src =" https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-change-retention-labels " aria-hidden =" true " />
148+ <img src =" https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-change-retention-labels " aria-hidden =" true " />
0 commit comments