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.
Source Code
  1. Add-Type -Path "xxxxx\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
  2. Add-Type -Path "xxxxx\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
  3. Add-Type -Path "xxxxx\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
  4. #Set parameter values
  5. $SiteURL="your site"
  6. $LibraryName="Library Name"
  7. $labelName=" your retension label name(Retain for 99 years)"
  8. #Setup Credentials to connect
  9. $Cred= Get-Credential
  10. $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  11. #Setup the context
  12. $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
  13. $Ctx.Credentials = $Credentials
  14. function GetSPODocLibraryFiles(){
  15. param
  16. (
  17. [Parameter(Mandatory=$true)] [string] $SiteURL
  18. )
  19. try{
  20. $Web = $Ctx.Web
  21. $Ctx.Load($web)
  22. #Get all immediate subsites of the site
  23. $Ctx.Load($web.Webs)
  24. $Ctx.executeQuery()
  25. #Call the function to Get Lists of the web
  26. Write-host "Processing Web :"$Web.URL
  27. write-host -f White "Connected to Web";
  28. }
  29. catch
  30. {
  31. $Message= "Processing Web :$Web.URL $($_.Exception.Message)"
  32. Write-Log -Message $Message -Level ERROR
  33. }
  34. #Loop All the lists or libraries if you want to apply it for all otherwise get the perticular library
  35. $Lists = $Web.Lists
  36. $Ctx.Load($Lists)
  37. $Ctx.ExecuteQuery()
  38. ForEach($Lib in $Lists)
  39. {
  40. #Get the List Name
  41. if($Lib | Where-Object { $_.BaseType -Eq "DocumentLibrary" })
  42. {
  43. Write-host $Lib.Title
  44. $Lib = $Ctx.Web.Lists.GetByTitle($LibraryName)
  45. $Ctx.Load($Lib)
  46. $Ctx.Load($Lib.RootFolder)
  47. $Ctx.ExecuteQuery()
  48. try
  49. {
  50. $listId = $Lib.Id
  51. $ie = New-Object -com InternetExplorer.Application
  52. try
  53. {
  54. $applyLabelUrl = "$($Web.URL)/_layouts/15/Hold.aspx?Tag=true&List={$($listId)}"
  55. Write-Host $applyLabelUrl
  56. $ie.visible = $true
  57. $ie.navigate($applyLabelUrl)
  58. for ($i = 0; $i -lt 300; $i++)
  59. {
  60. if ($ie.ReadyState -eq 4) { break }
  61. if ($i -eq 299) { throw "Unable to start IE session to simulate HP RM turning off inheritence." }
  62. }
  63. #Start-Sleep -Seconds 40
  64. $complianceTagDropDown = $ie.Document.getElementById('ctl00_PlaceHolderMain_inputFormSectionMain_ctl01_ComplianceTagDropDown')
  65. for ($i = 1; $i -lt $complianceTagDropDown.options.length; $i++)
  66. {
  67. if ($complianceTagDropDown.options[$i].text -eq $labelName)
  68. {
  69. $complianceTagDropDown.options[$i].selected = $true
  70. $isFound = $true
  71. break
  72. }
  73. }
  74. if ($complianceTagDropDown.id -ne "ctl00_PlaceHolderMain_inputFormSectionMain_ctl01_ComplianceTagDropDown")
  75. {
  76. throw "Didn't get assurance that I found complianceTagDropDown"
  77. }
  78. else
  79. {
  80. $isFound = $false
  81. for ($i = 1; $i -lt $complianceTagDropDown.options.length; $i++)
  82. {
  83. if ($complianceTagDropDown.options[$i].text -eq $labelName)
  84. {
  85. $complianceTagDropDown.options[$i].selected = $true
  86. $isFound = $true
  87. break
  88. }
  89. }
  90. if ($isFound -eq $false)
  91. {
  92. throw "Unable to select '$($labelName)' in complianceTagDropDown. No such option?"
  93. }
  94. $checkMark=$ie.Document.getElementById('ctl00_PlaceHolderMain_inputFormSectionMain_ctl01_DefaultComplianceTagSyncToListItems')
  95. $checkMark.checked =$true
  96. $saveButton = $ie.Document.getElementByID('ctl00_PlaceHolderMain_ctl00_RptControls_btnOK')
  97. if ($saveButton.id -ne "ctl00_PlaceHolderMain_ctl00_RptControls_btnOK")
  98. {
  99. throw "Didn't get assurance that I found saveButton"
  100. }
  101. else
  102. {
  103. $saveButton.click()
  104. }
  105. }
  106. }
  107. finally
  108. {
  109. $ie.Parent.Quit()
  110. Start-Sleep 1
  111. }
  112. }
  113. finally
  114. {
  115. }
  116. }
  117. }
  118. }
  119. GetSPODocLibraryFiles -SiteURL $SiteURL
Execute the program. Once it is executed successfully, you can see the label applied in the SharePoint List/Library.