From 710cf39a8e603793c12b86beaba1fb2e14913651 Mon Sep 17 00:00:00 2001 From: ojopiyo <122151392+ojopiyo@users.noreply.github.com> Date: Thu, 11 Dec 2025 21:23:08 +0000 Subject: [PATCH 1/2] Refactored spo-change-retention-labels script README.md Changes Made in the Refactored Script 1. Modular Design - Introduced a Set-RetentionLabel function to handle each list item, improving readability, testability, and ease of extension. 2. Retention Label Mapping Hashtable - Replaced multiple if/elseif statements with a hashtable ($RetentionLabelMapping), making it simple to add or update labels. 3. Early List Filtering - Only retrieves and processes lists of type GenericList or DocumentLibrary, reducing unnecessary iterations. 4. Improved Output Readability - Standardized Write-Host messages with consistent formatting and color coding for better visibility during execution. 5. Error Handling for Missing Fields - Skips lists that do not contain the _ComplianceTag field, preventing runtime errors. 6. Flattened Loops - Reduced nesting in loops, making the code easier to read, maintain, and debug. --- scripts/spo-change-retention-labels/README.md | 116 +++++++++++------- 1 file changed, 70 insertions(+), 46 deletions(-) diff --git a/scripts/spo-change-retention-labels/README.md b/scripts/spo-change-retention-labels/README.md index a56278ce6..bbe8458fc 100644 --- a/scripts/spo-change-retention-labels/README.md +++ b/scripts/spo-change-retention-labels/README.md @@ -58,55 +58,79 @@ Import-Module -Name pnp.powershell -DisableNameChecking $CsvFilePath = "\URLStoScan.csv" $SiteUrls = Import-Csv -Path $CsvFilePath | Select-Object -ExpandProperty SiteUrl -# Iterate through each SharePoint site URL +# Define retention label mapping +$RetentionLabelMapping = @{ + "Script Test 1 Old" = "Script Test 1 New" + "Script Test 2 Old" = "Script Test 2 New" + #"25 years" = "New 25 Years Label" + #"30 years" = "New 30 Years Label" + #"35 years" = "New 35 Years Label" +} + +# Function to process a single list item +function Set-RetentionLabel { + param ( + [Parameter(Mandatory)] + $List, + + [Parameter(Mandatory)] + $Item, + + [Parameter(Mandatory)] + $FieldInternalName, + + [Parameter(Mandatory)] + $Mapping + ) + + $CurrentLabel = $Item[$FieldInternalName] + Write-Host "Item ID: $($Item.Id) | Current label: $CurrentLabel" -ForegroundColor Blue + + if ($Mapping.ContainsKey($CurrentLabel)) { + $NewLabel = $Mapping[$CurrentLabel] + Set-PnPListItem -List $List -Identity $Item.Id -Label $NewLabel + Write-Host "Updated label to: $NewLabel" -ForegroundColor Green + } else { + Write-Host "No matching retention label found for Item ID $($Item.Id) in List '$($List.Title)'" -ForegroundColor Yellow + } +} + +# Iterate through each SharePoint site foreach ($SiteUrl in $SiteUrls) { - Write-Host "Processing SharePoint site: $SiteUrl" -ForegroundColor yellow - - # Connect to SharePoint site - Connect-PnPOnline -Url $SiteUrl -interactive - - # Retrieve all lists and libraries - $Lists = Get-PnPList - Write-Host "Lists Retrieved!" -ForegroundColor Green - foreach ($List in $Lists) { - if ($List.BaseType -eq "GenericList" -or $List.BaseType -eq "DocumentLibrary") { - # Retrieve all items in the list or library - Write-Host "Retrieving List items for list" -ForegroundColor yellow - $Items = Get-PnPListItem -List $List - - # Set retention label field - $RetentionLabelField = Get-PnPField -List $List -Identity "_ComplianceTag" - write-host "Retention label field is: $RetentionLabelField" - - foreach ($Item in $Items) { - Write-Host "Processing Item" -ForegroundColor yellow - $ExistingRetentionLabel = $Item[$RetentionLabelField.InternalName] - Write-Host "Current label is $ExistingRetentionLabel" -ForegroundColor Blue - - - if ($ExistingRetentionLabel -eq "Script Test 1 Old") { - Set-PnPListItem -List $List -Identity $Item.Id -label "Script Test 1 New" + Write-Host "Processing SharePoint site: $SiteUrl" -ForegroundColor Cyan + + Connect-PnPOnline -Url $SiteUrl -Interactive + + # Retrieve all lists and libraries + $Lists = Get-PnPList | Where-Object { $_.BaseType -in @("GenericList","DocumentLibrary") } + Write-Host "$($Lists.Count) lists/libraries retrieved." -ForegroundColor Green + + foreach ($List in $Lists) { + Write-Host "Processing list: $($List.Title)" -ForegroundColor Cyan + + # Get retention label field + $RetentionField = Get-PnPField -List $List -Identity "_ComplianceTag" + if (-not $RetentionField) { + Write-Host "Retention label field not found in list $($List.Title)." -ForegroundColor Red + continue } - elseif ($ExistingRetentionLabel -eq "Script Test 2 Old") { - Set-PnPListItem -List $List -Identity $Item.Id -label "Script Test 2 New" - } - #elseif ($ExistingRetentionLabel -eq "25 years") { - # Set-PnPListItem -List $List -Identity $Item.Id -Values @{ "_ComplianceTag" = "New 25 Years Label" } - #} - #elseif ($ExistingRetentionLabel -eq "30 years") { - # Set-PnPListItem -List $List -Identity $Item.Id -Values @{ "_ComplianceTag" = "New 30 Years Label" } - #} - #elseif ($ExistingRetentionLabel -eq "35 years") { - # Set-PnPListItem -List $List -Identity $Item.Id -Values @{ "_ComplianceTag" = "New 35 Years Label" } - #} - - else { - Write-Host "No matching retention label found for item $($Item.Id) in $($List.Title)." + + # Retrieve all items + $Items = Get-PnPListItem -List $List + Write-Host "$($Items.Count) items found in list $($List.Title)." -ForegroundColor Yellow + + foreach ($Item in $Items) { + Set-RetentionLabel -List $List -Item $Item -FieldInternalName $RetentionField.InternalName -Mapping $RetentionLabelMapping } + + Write-Host "Finished processing list $($List.Title)." -ForegroundColor Green } - - Write-Host "Retention labels set successfully." -}}} stop-Transcript + + Write-Host "Finished processing site $SiteUrl." -ForegroundColor Cyan +} + +Stop-Transcript + ``` [!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] @@ -120,4 +144,4 @@ foreach ($SiteUrl in $SiteUrls) { [!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] - \ No newline at end of file + From 07cfc71e9e6a1512c24764640c581dd725956c0d Mon Sep 17 00:00:00 2001 From: Paul Bullock Date: Thu, 18 Dec 2025 08:18:41 +0000 Subject: [PATCH 2/2] Added metadata and attribution --- scripts/spo-change-retention-labels/README.md | 3 ++- scripts/spo-change-retention-labels/assets/sample.json | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/scripts/spo-change-retention-labels/README.md b/scripts/spo-change-retention-labels/README.md index bbe8458fc..36deffcbd 100644 --- a/scripts/spo-change-retention-labels/README.md +++ b/scripts/spo-change-retention-labels/README.md @@ -140,7 +140,8 @@ Stop-Transcript | Author(s) | |-----------| -| Nick Brattoli| +| Nick Brattoli | +| ojopiyo | [!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] diff --git a/scripts/spo-change-retention-labels/assets/sample.json b/scripts/spo-change-retention-labels/assets/sample.json index ccdc9a59e..689ec6e11 100644 --- a/scripts/spo-change-retention-labels/assets/sample.json +++ b/scripts/spo-change-retention-labels/assets/sample.json @@ -9,7 +9,7 @@ "This will scan every SharePoint site (or OneDrive URL) and look at every item. When it does this, it will check the retention label for said item. If the retention label matches the hardcoded value, the label will be set to a new one." ], "creationDateTime": "2024-04-09", - "updateDateTime": "2024-04-09", + "updateDateTime": "2025-12-11", "products": [ "SharePoint" ], @@ -39,6 +39,12 @@ } ], "authors": [ + { + "gitHubAccount":"ojopiyo", + "company": "", + "pictureUrl": "https://avatars.githubusercontent.com/u/122151392?v=4", + "name": "ojopiyo" + }, { "gitHubAccount": "nbrattoli", "company": "",