SharePoint (2003 thru Online): Apply Sensitivity Label to all Document Libraries in a Site collection

Monday, June 10, 2024

Apply Sensitivity Label to all Document Libraries in a Site collection

E3 ($249) and E5($449) licenses have a big price gap, and E5 license has the advantage of auto labelling. Without it, we have to label everything manually, which is very time-consuming when we deal with many site collections and a lot of content.

For our project, I used a PowerShell Script to automate the process. I applied Sensitivity labels to all Site Collections first. 

Then, using the below PowerShell Script, I applied Sensitivity labels to Document libraries in each site collection. This way, the Office files that are uploaded or updated will get the label from the Document Library. However, we still need to label other file types manually.

List all the sites in a CSV file with URL as Header (As shown below).


symbol in PowerShell script is used for comments.

# Import the CSV file
$sites = Import-Csv -Path "D:\Dev_Sites_FilesWise.csv"

# Loop through each site
foreach ($site in $sites) {
    # Connect to the SharePoint site
    Connect-PnPOnline -Url $site.Url -Interactive

# The GUID of the 'Internal Use' sensitivity label
$Label = "Internal Use"

# Retrieve all document libraries (Except Style Library) from the site
$libraries = Get-PnPList | Where-Object {
  $_.BaseTemplate -eq [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary
    -and $_.Title -ne "Style Library"}

# Apply the sensitivity label to each document library if it doesn't already exist and print the name
foreach ($library in $libraries) {
 # Retrieve the current sensitivity label of the document library
 $currentLabel = (Get-PnPList -Identity $library.Id).DefaultSensitivityLabelForLibrary
   
  # Check if the sensitivity label is empty
  #if ($currentLabel -eq $null) {
      if ([string]::IsNullOrWhiteSpace($currentLabel)) {
      Set-PnPList -Identity $library.Id -DefaultSensitivityLabelForLibrary $Label
      Write-Host "'Internal Use' applied to: " $library.Title
    }
  else {
      Write-Host "Sensitivity label already exists on: " $library.Title
    }
}
}
# Disconnect the session
Disconnect-PnPOnline

No comments:

Post a Comment