Forum Discussion

Anonymous's avatar
Anonymous
Jan 21, 2018
Solved

Programmatically "Apply label to items in this list or library"

I need to be able to programmatically set the default ComplianceInfo for a document library which via the UI is Settings \ Apply label to items in this list or library (Apply Label). 

 

I can see CSOM methods for a list item via ListItemAllFields.ComplianceInfo \ SetComplianceTag but I need to act as if the user has chosen a default compliance Label for the Document Library.

 

Any ideas on what property or methods to use would be highly appreciated or perhaps a work around (other than a headless browser) would be great please.

 

I previously posted this at https://social.technet.microsoft.com/Forums/en-US/a0617140-5197-4908-8799-9e6021c8d70b/programmatically-apply-label-to-items-in-this-list-or-library?forum=onlineservicessharepoint but didn't get a real response ... surely this is possible / has been addressed by the PnP. Seems like a basic requirement.

  • Anonymous's avatar
    Anonymous
    Feb 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
    {
    }

5 Replies

  • geraint james's avatar
    geraint james
    Brass Contributor

    This was requested on https://sharepoint.uservoice.com/forums/329220-sharepoint-dev-platform/suggestions/33679303-support-compliancetag-on-the-csom-list-object and it looks like this was included in the March 2018 SharePoint Online CSOM update. Mikael is very active in PnP, so hopefully we'll see it added there soon....

     

     

    • Anonymous's avatar
      Anonymous

      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()

      • MSTest's avatar
        MSTest
        Copper Contributor

        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,

  • Anonymous's avatar
    Anonymous

    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
    {
    }
    • manidevelopment12345's avatar
      manidevelopment12345
      Copper Contributor

      Hi,

      $ie.Document.getElementByID("ctl00_PlaceHolderMain_inputFormSectionMain_ctl01_ComplianceTagDropDown")

      above line is not returning any value its giving empty .

       

      Please help me out if you have any solution

       

      Thanks in adavance

       

Resources