Forum Discussion
Programmatically "Apply label to items in this list or library"
- AnonymousFeb 14, 2018
In the end I resorted to PowerShell:
try { $listId = (Get-PnPList "Shared Documents").Id $ie = New-Object -com InternetExplorer.Application try { $applyLabelUrl = "$($spUrl)/_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 } Start-Sleep 1 if ($i -eq 299) { throw "Unable to start IE session to simulate HP RM turning off inheritence." } } Start-Sleep 2 $complianceTagDropDown = $ie.Document.getElementByID('ctl00_PlaceHolderMain_inputFormSectionMain_ctl01_ComplianceTagDropDown') 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?" } $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 { }
Came back to this a whilst later. Yes it was indeed included. When I refactored I found this article useful http://www.myfatblog.co.uk/index.php/2018/05/configuring-default-office-365-labels-using-powershell/. Shame I can't see a parameter to apply to existing documents but that wasn't too hard to do using a loop.
Applying to the document library as a default:
$complianceTags = [Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy]::GetAvailableTagsForSite($spoContext,$spoContext.Url)
$spoContext.ExecuteQuery()
$complianceTag = $complianceTags | Where-Object { $_.TagName -eq $complianceTagName }
$doclib = Get-PnPList -Identity $docLibraryName -Includes RootFolder
[Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy]::SetListComplianceTag($spoContext, $docLib.RootFolder.ServerRelativeUrl, $complianceTagName, $complianceTag.BlockDelete, $complianceTag.BlockEdit, $complianceTag.IsEventTag)
$spoContext.ExecuteQuery()
Hi Paul,
Can you please advise on a way for me to apply separate tags for individual items through script. Similar to the way we generally do by navigating to the item in the library -->Selecting retention label to apply
Regards,