Introduction
This post explains how to apply a label at list/library level in SharePoint Online by using Client Side Object Model in PowerShell.
This script will help save us developers a lot of time in getting all the lists/libraries from a individual Site Colletion or subsites. So, here is the script.
- Copy the below code to a .ps1 file.
- Run the .ps1 file on the SharePoint PowerShell modules.
- Configure the input parameters.
- Add-Type -Path "xxxxx\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "xxxxx\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
- Add-Type -Path "xxxxx\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
- #Set parameter values
- $SiteURL="your site"
- $LibraryName="Library Name"
- $labelName=" your retension label name(Retain for 99 years)"
- #Setup Credentials to connect
- $Cred= Get-Credential
- $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
- #Setup the context
- $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
- $Ctx.Credentials = $Credentials
- function GetSPODocLibraryFiles(){
- param
- (
- [Parameter(Mandatory=$true)] [string] $SiteURL
- )
- try{
- $Web = $Ctx.Web
- $Ctx.Load($web)
- #Get all immediate subsites of the site
- $Ctx.Load($web.Webs)
- $Ctx.executeQuery()
- #Call the function to Get Lists of the web
- Write-host "Processing Web :"$Web.URL
- write-host -f White "Connected to Web";
- }
- catch
- {
- $Message= "Processing Web :$Web.URL $($_.Exception.Message)"
- Write-Log -Message $Message -Level ERROR
- }
- #Loop All the lists or libraries if you want to apply it for all otherwise get the perticular library
- $Lists = $Web.Lists
- $Ctx.Load($Lists)
- $Ctx.ExecuteQuery()
- ForEach($Lib in $Lists)
- {
- #Get the List Name
- if($Lib | Where-Object { $_.BaseType -Eq "DocumentLibrary" })
- {
- Write-host $Lib.Title
- $Lib = $Ctx.Web.Lists.GetByTitle($LibraryName)
- $Ctx.Load($Lib)
- $Ctx.Load($Lib.RootFolder)
- $Ctx.ExecuteQuery()
- try
- {
- $listId = $Lib.Id
- $ie = New-Object -com InternetExplorer.Application
- try
- {
- $applyLabelUrl = "$($Web.URL)/_layouts/15/Hold.aspx?Tag=true&List={$($listId)}"
- Write-Host $applyLabelUrl
- $ie.visible = $true
- $ie.navigate($applyLabelUrl)
- for ($i = 0; $i -lt 300; $i++)
- {
- if ($ie.ReadyState -eq 4) { break }
- if ($i -eq 299) { throw "Unable to start IE session to simulate HP RM turning off inheritence." }
- }
- #Start-Sleep -Seconds 40
- $complianceTagDropDown = $ie.Document.getElementById('ctl00_PlaceHolderMain_inputFormSectionMain_ctl01_ComplianceTagDropDown')
- for ($i = 1; $i -lt $complianceTagDropDown.options.length; $i++)
- {
- if ($complianceTagDropDown.options[$i].text -eq $labelName)
- {
- $complianceTagDropDown.options[$i].selected = $true
- $isFound = $true
- break
- }
- }
- if ($complianceTagDropDown.id -ne "ctl00_PlaceHolderMain_inputFormSectionMain_ctl01_ComplianceTagDropDown")
- {
- throw "Didn't get assurance that I found complianceTagDropDown"
- }
- else
- {
- $isFound = $false
- for ($i = 1; $i -lt $complianceTagDropDown.options.length; $i++)
- {
- if ($complianceTagDropDown.options[$i].text -eq $labelName)
- {
- $complianceTagDropDown.options[$i].selected = $true
- $isFound = $true
- break
- }
- }
- if ($isFound -eq $false)
- {
- throw "Unable to select '$($labelName)' in complianceTagDropDown. No such option?"
- }
- $checkMark=$ie.Document.getElementById('ctl00_PlaceHolderMain_inputFormSectionMain_ctl01_DefaultComplianceTagSyncToListItems')
- $checkMark.checked =$true
- $saveButton = $ie.Document.getElementByID('ctl00_PlaceHolderMain_ctl00_RptControls_btnOK')
- if ($saveButton.id -ne "ctl00_PlaceHolderMain_ctl00_RptControls_btnOK")
- {
- throw "Didn't get assurance that I found saveButton"
- }
- else
- {
- $saveButton.click()
- }
- }
- }
- finally
- {
- $ie.Parent.Quit()
- Start-Sleep 1
- }
- }
- finally
- {
- }
- }
- }
- }
- GetSPODocLibraryFiles -SiteURL $SiteURL
Execute the program. Once it is executed successfully, you can see the label applied in the SharePoint List/Library.

MILIND RANKAPosted May 24, 2020, 1:53 AM
Hello Mani, I cannot add a newline in this comment hence the fourth attempt. I am new to Sharepoint and would like to programatically add attributes maintained in EXCEL sheet to document attributes uploaded in Sharepoint location which can be uniqurly identifid using an attribute call opportunity id (my defined one). This code given looks a little bit compicated, hence nee your help.
Huy NguyenPosted May 20, 2020, 10:43 PM
Nice to meet you.