Forum Discussion

Brian Jacobsen's avatar
Brian Jacobsen
Copper Contributor
Feb 08, 2018
Solved

Apply default compliance label to document library from code (SPO)

Targeting Office 365 / SharePoint Online, we are creating labels in the Office 365 compliance center, and would like to assign these as default labels in document libraries through code (e.g. CSOM/REST/Graph/PnP)

 

Is this possible, and are there any samples on this available?

 

I am looking at the Microsoft.SharePoint.Client.CompliancePolicy namespace, but the documentation is limited at best.

9 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 too....

    • Brian Jacobsen's avatar
      Brian Jacobsen
      Copper Contributor

      Perfect - thanks, hadn't seen that uservoice entry :)

       

      /Brian

    • Brian Jacobsen's avatar
      Brian Jacobsen
      Copper Contributor

      Hi Juan Carlos,

       

      thanks hadn't seen that one - I will check it out and put a result here :)

       

      /Brian

       

       

      • Brian Jacobsen's avatar
        Brian Jacobsen
        Copper Contributor

        I can see that I can set the label on a document but I cannot see how to change the default label for a document library - the below will change the label of a document:

         

        $list = $ctx.Web.GetList("/sites/somesite/somedoclib")
        $ctx.Load($list)
        $ctx.ExecuteQuery()
        $item = $list.GetItemById(DocumentItemID)
        $ctx.Load($item)
        $ctx.ExecuteQuery()
        $item.SetComplianceTagWithNoHold("MyLabel")
        $item.Update()
        $list.Update()
        $ctx.ExecuteQuery()
        

         

        I will consider to add an ItemAdded remote event receiver and set the label when an item is added to the library (allowing the end user to change it if required) - but thanks for the pointer

         

Resources