Forum Discussion

Deleted's avatar
Deleted
Jan 21, 2018

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...
  • Deleted's avatar
    Deleted
    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
    {
    }

Resources