SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-153707%22%20slang%3D%22en-US%22%3EApply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-153707%22%20slang%3D%22en-US%22%3E%3CP%3ETargeting%20Office%20365%20%2F%20SharePoint%20Online%2C%20we%20are%20creating%20labels%20in%20the%20Office%20365%20compliance%20center%2C%20and%20would%20like%20to%20assign%20these%20as%20default%20labels%20in%20document%20libraries%20through%20code%20(e.g.%20CSOM%2FREST%2FGraph%2FPnP)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIs%20this%20possible%2C%20and%26nbsp%3Bare%20there%20any%20samples%20on%20this%20available%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20am%20looking%20at%20the%20Microsoft.SharePoint.Client.CompliancePolicy%20namespace%2C%20but%20the%20documentation%20is%20limited%20at%20best.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-185176%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-185176%22%20slang%3D%22en-US%22%3E%3CP%3EPerfect%20-%20thanks%2C%20hadn't%20seen%20that%20uservoice%20entry%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%2FBrian%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-185160%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-185160%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EThis%20was%20requested%20on%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fsharepoint.uservoice.com%2Fforums%2F329220-sharepoint-dev-platform%2Fsuggestions%2F33679303-support-compliancetag-on-the-csom-list-object%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EUserVoice%3C%2FA%3E%3CSPAN%3E%20and%20it%20looks%20like%20this%20was%20included%20in%20the%20March%202018%20SharePoint%20Online%20CSOM%20update.%20Mikael%20is%20very%20active%20in%20PnP%2C%20so%20hopefully%20we'll%20see%20it%26nbsp%3Badded%20there%20soon%20too....%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-154067%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-154067%22%20slang%3D%22en-US%22%3E%3CP%3EYes%20-%20that%20is%20what%20I'm%20considering%20with%20a%20webhook%20-%26gt%3B%20I'll%20poste%20a%20note%20when%20it's%20done%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-153995%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-153995%22%20slang%3D%22en-US%22%3EInteresting...what%20about%20an%20Azure%20Function%20that%20is%20responsible%20of%20making%20that%20job%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-153992%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-153992%22%20slang%3D%22en-US%22%3E%3CP%3EI%20will%20consider%20webhooks%20as%20well%2C%20but%20as%20this%20will%20be%20a%20large%20number%20of%20site%20collections%20and%20document%20libraries%20(read%3A%20all%20new%20team%20sites%20created)%2C%20then%20a%20MS%20Flow%20dedicated%20to%20each%20wouldn't%20be%20feasible%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20keep%20the%20ideas%20coming%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-153989%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-153989%22%20slang%3D%22en-US%22%3E%3CP%3EWhat%20about%20using%20Flow%20or%20Webhooks%20instead%20of%20a%20Remote%20Event%20Receiver%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-153940%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-153940%22%20slang%3D%22en-US%22%3E%3CP%3EI%20can%20see%20that%20I%20can%20set%20the%20label%20on%20a%20document%20but%20I%20cannot%20see%20how%20to%20change%20the%20default%20label%20for%20a%20document%20library%20-%20the%20below%20will%20change%20the%20label%20of%20a%20document%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%24list%20%3D%20%24ctx.Web.GetList(%22%2Fsites%2Fsomesite%2Fsomedoclib%22)%0A%24ctx.Load(%24list)%0A%24ctx.ExecuteQuery()%0A%24item%20%3D%20%24list.GetItemById(DocumentItemID)%0A%24ctx.Load(%24item)%0A%24ctx.ExecuteQuery()%0A%24item.SetComplianceTagWithNoHold(%22MyLabel%22)%0A%24item.Update()%0A%24list.Update()%0A%24ctx.ExecuteQuery()%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20will%20consider%20to%20add%20an%20ItemAdded%20remote%20event%20receiver%20and%20set%20the%20label%20when%20an%20item%20is%20added%20to%20the%20library%20(allowing%20the%20end%20user%20to%20change%20it%20if%20required)%20-%20but%20thanks%20for%20the%20pointer%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-153768%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-153768%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Juan%20Carlos%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ethanks%20hadn't%20seen%20that%20one%20-%20I%20will%20check%20it%20out%20and%20put%20a%20result%20here%20%3A)%3C%2Fimg%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%2FBrian%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-153761%22%20slang%3D%22en-US%22%3ERe%3A%20Apply%20default%20compliance%20label%20to%20document%20library%20from%20code%20(SPO)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-153761%22%20slang%3D%22en-US%22%3EYeap%2C%20not%20to%20much%20information%20about%20this...have%20you%20read%20this%3A%20%3CA%20href%3D%22https%3A%2F%2Fsocial.technet.microsoft.com%2FForums%2Fen-US%2Fa0617140-5197-4908-8799-9e6021c8d70b%2Fprogrammatically-apply-label-to-items-in-this-list-or-library%3Fforum%3Donlineservicessharepoint%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsocial.technet.microsoft.com%2FForums%2Fen-US%2Fa0617140-5197-4908-8799-9e6021c8d70b%2Fprogrammatically-apply-label-to-items-in-this-list-or-library%3Fforum%3Donlineservicessharepoint%3C%2FA%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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
best response confirmed by Brian Jacobsen (Occasional Contributor)

Hi Juan Carlos,

 

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

 

/Brian

 

 

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

 

What about using Flow or Webhooks instead of a Remote Event Receiver?

I will consider webhooks as well, but as this will be a large number of site collections and document libraries (read: all new team sites created), then a MS Flow dedicated to each wouldn't be feasible

 

But keep the ideas coming :)

Interesting...what about an Azure Function that is responsible of making that job?

Yes - that is what I'm considering with a webhook -> I'll poste a note when it's done :)

This was requested on UserVoice 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....

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

 

/Brian