Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: Programmatically “Apply Label To Items In List Or Library”
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
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.
Print article | This entry was posted by Peter on May 20, 2020 at 4:14 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |