SharePoint (2003 thru Online): June 2024

Thursday, June 20, 2024

Sensitivity labels Properties of all files in a Document library

Copilot is a big deal on Microsoft 365 these days. Lots of people are using it, and Sensitivity labels are getting more popular too. I had to run a PowerShell Script to get the Properties of all files in a Document library, including their Sensitivity labels. PnP PowerShell made it easy for me.

I ran the script using the PowerShell extension in Visual Studio Code.

# Connect to the SharePoint site
$siteURL = "https://gurram.sharepoint.com/sites/dev"
Connect-PnPOnline -Url $siteURL -Interactive

# The name of the document library
$libraryName = "test1"

# Retrieve all list items in the document library
$items = Get-PnPListItem -List $libraryName

# Loop through each item and retrieve FieldValuesForEdit
foreach ($item in $items) {
    # Retrieve the FieldValuesForEdit property of the list item
    $fieldValuesForEdit = Get-PnPProperty -ClientObject $item -Property "FieldValuesForEdit"
    Write-Host "Item ID:" $item.Id
    Write-Host "FieldValuesForEdit:"
    $fieldValuesForEdit.FieldValues.FileRef #FileReferenceLink
    $fieldValuesForEdit.FieldValues._IpLabelId #Label ID
    $fieldValuesForEdit.FieldValues._DisplayName #Label Display Name
}

Below screenshot shows the result.


Monday, June 10, 2024

Apply Sensitivity label to all Site collections in SharePoint admin center

With E3 license, we have to label everything manually, which is very time-consuming when we deal with many site collections.

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

#Run the Get-Label command to retrieve the list of available labels:
Connect-IPPSSession -UserPrincipalName spadmin@gurram.onmicrosoft.com
Get-Label |ft Name, Guid, ContentType
#Copy the required label GUID
Disconnect-IPPSSession
--------------------------------------------------------------
#Connect to PnP PowerShell
Connect-PnPOnline -Url "https://gurram-admin.sharepoint.com" -Interactive

#Get All Site collections - Include Only: Team site (no M365 Group), Team site (classic experience), Project Web App site and Team sites
$Sites = Get-PnPTenantSite | Where-Object { $_.Template -eq "STS#3" -or $_.Template -eq "STS#0" -or $_.Template -eq "PWA#0" -or $_.Template -eq "GROUP#0" }

#For each site in the Site collections
ForEach ($site in $Sites) {
    #Required Label GUID from the above Script
    $LabelId = "abc123de-45f6-7g89-hi12-34j56789jk12"  
    $ctn = Connect-PnPOnline -Url $site.URL -Interactive
    $label = Get-PnPSiteSensitivityLabel -Connection $ctn

   if ($label.DisplayName -eq "") {
   #Add sensitivity Label to site
   Set-PnPTenantSite -Identity $site.URL -SensitivityLabel $LabelId
  } else {
    Write-Host $site.URL,"Not Blank"
  }
   $Object = [PSCustomObject]@{
    URL = $site.URL
    Sensitivitylabel= $label.DisplayName
    }
    $List += $Object
    #Write-Host $site.URL
    }
#Disconnect
Disconnect-PnPOnline

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