Skip to content

Commit caab95b

Browse files
authored
Merge pull request #892 from ojopiyo/patch-2
Refactored spo-change-retention-labels script README.md
2 parents 935b752 + 07cfc71 commit caab95b

File tree

2 files changed

+79
-48
lines changed

2 files changed

+79
-48
lines changed

scripts/spo-change-retention-labels/README.md

Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6299
foreach ($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" />

scripts/spo-change-retention-labels/assets/sample.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"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."
1010
],
1111
"creationDateTime": "2024-04-09",
12-
"updateDateTime": "2024-04-09",
12+
"updateDateTime": "2025-12-11",
1313
"products": [
1414
"SharePoint"
1515
],
@@ -39,6 +39,12 @@
3939
}
4040
],
4141
"authors": [
42+
{
43+
"gitHubAccount":"ojopiyo",
44+
"company": "",
45+
"pictureUrl": "https://avatars.githubusercontent.com/u/122151392?v=4",
46+
"name": "ojopiyo"
47+
},
4248
{
4349
"gitHubAccount": "nbrattoli",
4450
"company": "",

0 commit comments

Comments
 (0)