Home
%3CLINGO-SUB%20id%3D%22lingo-sub-242412%22%20slang%3D%22en-US%22%3EScripted%20Application%20of%20Data%20Governance%20Labels%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-242412%22%20slang%3D%22en-US%22%3E%3CP%3E(Today's%20post%20comes%20from%20Robin%20Meure%20in%20Microsoft%20Premier%20Field%20Engineering%20-%20many%20thanks!)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EHow%20to%20apply%20Office%20365%20labels%20programmatically%2Fscripted%20to%20a%20document%20library%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F44886iE38CEC15681858C0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22ADGLabel.jpg%22%20title%3D%22ADGLabel.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EWhile%20we%20had%20the%20ability%20to%20make%20labels%20available%20to%20certain%20(or%20all)%20sites%20from%20the%20Office%20365%20Security%20%26amp%3B%20Compliance%20Center%20and%20then%20apply%20a%20specific%20label%20to%20a%20document%20library%20like%20in%20the%20UI%20(as%20can%20been%20seen%20in%20the%20screenshot%20above)%2C%20there%20was%20no%20way%20to%20set%20a%20default%20label%20onto%20a%20document%20library%20for%20each%20site%20(at%20least%20not%20in%20a%20scripted%20or%20programmatic%20way%20%3A)%3C%2Fimg%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESince%20the%20release%20of%20the%20%3CA%20href%3D%22https%3A%2F%2Femea01.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdev.office.com%252Fblogs%252Fnew-sharepoint-csom-version-released-for-Office-365-march-2018%26amp%3Bdata%3D02%257C01%257C%257C785ce2df9814411200f308d5bb13c740%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636620615966708062%26amp%3Bsdata%3DEFoTu5YDvXhS9Bss9v%252BPSIm6Ofxg%252BgAf1cUGL3ASYZU%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMarch%202018%20CSOM%20package%3C%2FA%3E%2C%20an%20API%20has%20been%20exposed%20to%20make%20this%20happen.%3C%2FP%3E%0A%3CP%3EIn%20short%2C%20the%20following%20methods%20have%20been%20exposed%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Epublic%20method%20Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.GetListComplianceTag%3C%2FLI%3E%0A%3CLI%3Epublic%20method%20Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.SetListComplianceTag%3C%2FLI%3E%0A%3CLI%3Epublic%20method%20Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.SetListComplianceTagWithMetaInfo%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20how%20we%20can%20call%20it%20from%20PowerShell%3A%3C%2FP%3E%0A%3CP%3E%5BMicrosoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy%5D%3A%3ASetListComplianceTag(%24web.Context%2C%24listUrl%2C%24nameOfPolicy%2C%24false%2C%24false%2C%24false)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20parameters%20of%20the%20methods%20are%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3ESetListComplianceTag%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3E(%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3EMicrosoft.SharePoint.Client.ClientRuntimeContext%20context%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3Estring%20listUrl%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3Estring%20complianceTagValue%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3Ebool%20blockDelete%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3Ebool%20blockEdit%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3Ebool%20syncToItems%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3E)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22147%22%3E%3CP%3EListUrl%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22474%22%3E%3CP%3EUse%20the%20RootFolder.ServerRelativeUrl%20property%20of%20the%20List%20object%20to%20fetch%20this.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22149%22%3E%3CP%3EComplianceTagValue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22466%22%3E%3CP%3EThe%20%3CSTRONG%3Etitle%3C%2FSTRONG%3E%20of%20the%20Label%20you%20want%20to%20apply%20on%20the%20document%20library%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22147%22%3E%3CP%3EBlockDelete%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22468%22%3E%3CP%3EBlocks%20the%20ability%20to%20delete%20the%20document%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22147%22%3E%3CP%3EBlockEdit%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22468%22%3E%3CP%3EBlocks%20the%20ability%20to%20edit%20the%20document%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22147%22%3E%3CP%3ESyncToItems%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22468%22%3E%3CP%3EApply%20label%20to%20existing%20items%20in%20the%20library.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20is%20a%20snippet%20to%20apply%20a%20label%20to%20a%20default%20library%20by%20either%20using%20the%20SharePointPnPPowerShellOnline%20module%20which%20can%20be%20found%20at%20%3CA%20href%3D%22https%3A%2F%2Femea01.safelinks.protection.outlook.com%2F%3Furl%3Dhttp%253A%252F%252Faka.ms%252Fsppnp-powershell%26amp%3Bdata%3D02%257C01%257C%257C785ce2df9814411200f308d5bb13c740%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636620615966738081%26amp%3Bsdata%3DBRW4NeOGFE9pAlYIjUhFyp0YAcAq0B6uxS4wHrYcYec%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPnP%20PowerShell%3C%2FA%3E%26nbsp%3Brepository%20or%20just%20'vanilla'%20PowerShell%20CSOM.%20The%20most%20important%20thing%20is%20to%20remember%20that%20we%20need%20the%20right%20version%20of%20the%20CSOM%20library%20to%20be%20loaded.%20By%20default%20the%20PnPPowerShell%2C%20when%20being%20updated%20of%20course%2C%20will%20have%20the%20latest%20CSOM%20assemblies%20(as%20opposed%20to%20the%20SharePoint%20Online%20Management%20Shell).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%20Variables%20to%20be%20replaced%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3E%23url%20of%20the%20web%20you%20want%20to%20apply%20the%20label%20on%3C%2FP%3E%0A%3CP%3E%24url%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Femea01.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Ftenant.sharepoint.com%26amp%3Bdata%3D02%257C01%257C%257C785ce2df9814411200f308d5bb13c740%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636620615966738081%26amp%3Bsdata%3Dz6pGTBOT0OmqB6u9x2bStZsn%252BM0gGrUkZrQ5juvUn1o%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftenant.sharepoint.com%3C%2FA%3E%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23name%20of%20the%20document%20library%20you%20want%20to%20apply%20the%20label%20on%3C%2FP%3E%0A%3CP%3E%24listName%20%3D%20%22Documents%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23credentials%20being%20used%20to%20connect%20to%20the%20web%3C%2FP%3E%0A%3CP%3E%24credentials%20%3D%20Get-Credential%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23title%20of%20the%20label%20you%20want%20to%20apply%3C%2FP%3E%0A%3CP%3E%23(it%20actually%20requires%20the%20title%20of%20the%20policy%2C%20as%20opposed%20to%20a%20Id%2FGUID..)%3C%2FP%3E%0A%3CP%3E%24nameOfPolicy%20%3D%22Project%20documentation%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%20%2FVariables%20to%20be%20replaced%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%20PnP%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3EConnect-PnPOnline%20-Url%20%24url%3C%2FP%3E%0A%3CP%3E%24web%20%3D%20Get-PnPWeb%3C%2FP%3E%0A%3CP%3E%24context%20%3D%20%24web.Context%3C%2FP%3E%0A%3CP%3E%24list%20%3D%20Get-PnpList%20-Identity%20%24listName%20-Web%20%24web%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%20%2FPnP%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%20Vanilla%20CSOM%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3E%23%20Need%20at%20least%2016.1.7521.1200%20(March%202018%20CSOM)%3C%2FP%3E%0A%3CP%3E%23%20Download%20the%20CSOM%20assemblies%20from%20%3CA%20href%3D%22https%3A%2F%2Femea01.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fwww.nuget.org%252Fpackages%252FMicrosoft.SharePointOnline.CSOM%26amp%3Bdata%3D02%257C01%257C%257C785ce2df9814411200f308d5bb13c740%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636620615966738081%26amp%3Bsdata%3DHvoFj%252FANp%252Bdz3Sa0ljo0RG1UM4Kqp4dh%252BsWOybKV1K0%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.nuget.org%2Fpackages%2FMicrosoft.SharePointOnline.CSOM%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CWindowsPowerShell%5CModules%5CSharePointPnPPowerShellOnline%5CMicrosoft.SharePoint.Client.dll%22%3C%2FP%3E%0A%3CP%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CWindowsPowerShell%5CModules%5CSharePointPnPPowerShellOnline%5CMicrosoft.SharePoint.Client.Runtime.dll%22%3C%2FP%3E%0A%3CP%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CWindowsPowerShell%5CModules%5CSharePointPnPPowerShellOnline%5CMicrosoft.Office.Client.Policy.dll%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%24context%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24url)%3C%2FP%3E%0A%3CP%3E%24context.Credentials%20%3D%26nbsp%3B%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24credentials.UserName%2C%20%24credentials.Password)%3C%2FP%3E%0A%3CP%3E%24list%20%3D%20%24context.Web.Lists.GetByTitle(%24listName)%3C%2FP%3E%0A%3CP%3E%24context.Load(%24list)%3C%2FP%3E%0A%3CP%3E%24context.Load(%24list.RootFolder)%3C%2FP%3E%0A%3CP%3E%24context.ExecuteQuery()%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%20%2FVanilla%20CSOM%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%20Main%20part%20of%20the%20script%20%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20To%20get%20the%20Url%20of%20the%20list%2C%20we're%20fetching%20the%20RootFolder%20which%20contains%20the%20ServerRelativeUrl%20property%3C%2FP%3E%0A%3CP%3E%24listUrl%20%3D%20%24list.RootFolder.ServerRelativeUrl%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20Calling%20the%20method%20to%20SET%20the%20tag%3C%2FP%3E%0A%3CP%3E%5BMicrosoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy%5D%3A%3ASetListComplianceTag(%24context%2C%24listUrl%2C%24nameOfPolicy%2C%24false%2C%24false%2C%24false)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Call%20the%20executequery%20method%20on%20the%20Context%20to%20apply%20the%20change%3C%2FP%3E%0A%3CP%3E%24context.ExecuteQuery()%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%20%2FMain%20part%20of%20the%20script%20%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20you%20go!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-242412%22%20slang%3D%22en-US%22%3E%3CP%3EQuick%20walkthrough%20about%20using%20PowerShell%20to%20set%20default%20labels%20on%20SharePoint%20document%20libraries.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-242412%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ETips%20%26amp%3B%20Tricks%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-260174%22%20slang%3D%22en-US%22%3ERe%3A%20Scripted%20Application%20of%20Data%20Governance%20Labels%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-260174%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20great!%20When%20can%20we%20expect%20official%20documentation%20on%20docs.microsoft.com%20on%20this%20namespace%3F%20An%20article%20like%20this%20is%20great%2C%20but%20official%20documentation%20even%20better%20%3B)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-469789%22%20slang%3D%22en-US%22%3ERe%3A%20Scripted%20Application%20of%20Data%20Governance%20Labels%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-469789%22%20slang%3D%22en-US%22%3E%3CP%3EUnfortunately%2C%20after%20coding%20an%20enterprise%20solution%20around%20this%20beware%20that%20it%20doesn't%20work.%20Using%20this%20API%20method%20has%20resulted%20in%20documents%20not%20appearing%20in%20the%20Disposition%20UI.%20Support%20call%20with%20Microsoft%20comment%20was%20that%20the%20Project%20Team%20have%20specifically%20said%20not%20to%20use%20this%20API%20and%20to%20only%20use%20the%20UI.%20When%20challenged%20I%20was%20told%20that%20UI%20wasn't%20cool%20for%20an%20enterprise%20and%20we%20wanted%20to%20set%20it%20for%20the%20users%20(i.e.%20via%20the%20API)%20they%20were%20definitive%20to%20not%20use%20the%20API%20and%20closed%20the%20call.%20Automation%20through%20headless%20scripting%20is%20perhaps%20my%20next%20step.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

(Today's post comes from Robin Meure in Microsoft Premier Field Engineering - many thanks!)

 

How to apply Office 365 labels programmatically/scripted to a document library

 ADGLabel.jpg

While we had the ability to make labels available to certain (or all) sites from the Office 365 Security & Compliance Center and then apply a specific label to a document library like in the UI (as can been seen in the screenshot above), there was no way to set a default label onto a document library for each site (at least not in a scripted or programmatic way :)

 

Since the release of the March 2018 CSOM package, an API has been exposed to make this happen.

In short, the following methods have been exposed:

 

  • public method Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.GetListComplianceTag
  • public method Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.SetListComplianceTag
  • public method Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.SetListComplianceTagWithMetaInfo

 

Here is how we can call it from PowerShell:

[Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy]::SetListComplianceTag($web.Context,$listUrl,$nameOfPolicy,$false,$false,$false)

 

The parameters of the methods are the following:

SetListComplianceTag

(

Microsoft.SharePoint.Client.ClientRuntimeContext context,

string listUrl,

string complianceTagValue,

bool blockDelete,

bool blockEdit,

bool syncToItems

)

 

ListUrl

Use the RootFolder.ServerRelativeUrl property of the List object to fetch this.

ComplianceTagValue

The title of the Label you want to apply on the document library

BlockDelete

Blocks the ability to delete the document

BlockEdit

Blocks the ability to edit the document

SyncToItems

Apply label to existing items in the library.

 

Next is a snippet to apply a label to a default library by either using the SharePointPnPPowerShellOnline module which can be found at PnP PowerShell repository or just 'vanilla' PowerShell CSOM. The most important thing is to remember that we need the right version of the CSOM library to be loaded. By default the PnPPowerShell, when being updated of course, will have the latest CSOM assemblies (as opposed to the SharePoint Online Management Shell).

 

############# Variables to be replaced ###############

#url of the web you want to apply the label on

$url = "https://tenant.sharepoint.com"

 

#name of the document library you want to apply the label on

$listName = "Documents"

 

#credentials being used to connect to the web

$credentials = Get-Credential

 

#title of the label you want to apply

#(it actually requires the title of the policy, as opposed to a Id/GUID..)

$nameOfPolicy ="Project documentation"

 

############# /Variables to be replaced ###############

 

############# PnP ###############

Connect-PnPOnline -Url $url

$web = Get-PnPWeb

$context = $web.Context

$list = Get-PnpList -Identity $listName -Web $web

############# /PnP ###############

 

############# Vanilla CSOM ###############

# Need at least 16.1.7521.1200 (March 2018 CSOM)

# Download the CSOM assemblies from https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM

Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\Microsoft.SharePoint.Client.Runtime.dll"

Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\Microsoft.Office.Client.Policy.dll"

 

$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)

$context.Credentials =  New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.UserName, $credentials.Password)

$list = $context.Web.Lists.GetByTitle($listName)

$context.Load($list)

$context.Load($list.RootFolder)

$context.ExecuteQuery()

############# /Vanilla CSOM ###############

 

############ Main part of the script ############

 

# To get the Url of the list, we're fetching the RootFolder which contains the ServerRelativeUrl property

$listUrl = $list.RootFolder.ServerRelativeUrl

 

# Calling the method to SET the tag

[Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy]::SetListComplianceTag($context,$listUrl,$nameOfPolicy,$false,$false,$false)

 

#Call the executequery method on the Context to apply the change

$context.ExecuteQuery()

 

############ /Main part of the script ############

 

There you go!

2 Comments
Occasional Contributor

This is great! When can we expect official documentation on docs.microsoft.com on this namespace? An article like this is great, but official documentation even better ;)

Deleted
Not applicable

Unfortunately, after coding an enterprise solution around this beware that it doesn't work. Using this API method has resulted in documents not appearing in the Disposition UI. Support call with Microsoft comment was that the Project Team have specifically said not to use this API and to only use the UI. When challenged I was told that UI wasn't cool for an enterprise and we wanted to set it for the users (i.e. via the API) they were definitive to not use the API and closed the call. Automation through headless scripting is perhaps my next step.