Sep 29 2017 12:02 AM
I have created a few compliance labels from the Office 365 Security & Compliance portal. These have been successfully published to one of my SharePoint sites and I can apply it to document from the modern library UI. My understanding from the august 2017 CSOM release notes was that it should be possible to get the available labels in a site using the GetComplianceTag methods. I have not been successful i doing this however. One approach causes a exception saying I need to enable the Site Policy feature, even though it is enabled. The other return a empty result.
Is there a working example for getting available classification tags programatically?
See code example below:
using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.CompliancePolicy; using System; using System.Collections.Generic; using System.Linq; using System.Security; namespace Policies { class Program { static void Main(string[] args) { using (var clientContext = CreateClientContext()) { //Returns empty var tags = GetAvailableTagsForSite(clientContext); foreach (var tag in tags) { Console.WriteLine(tag); } // Throws "Site Policy feature not activated in site collection" even if it is activated var tags = GetComplianceTags(clientContext); foreach (var tag in tags) { Console.WriteLine(tag); } Console.ReadLine(); } } private static ClientContext CreateClientContext() { var password = new SecureString(); foreach (var c in "foobar") password.AppendChar(c); var credentials = new SharePointOnlineCredentials("admin@contoso.onmicrosoft.com", password); return new ClientContext("https://contoso.sharepoint.com/sites/gdpr") { Credentials = credentials }; } private static IEnumerable<string> GetComplianceTags(ClientContext clientContext) { var web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); var policyStore = new SPPolicyStore(clientContext, web); var tags = policyStore.GetComplianceTags(); clientContext.ExecuteQuery(); return tags.Select(t => t.TagName); } private static IEnumerable<string> GetAvailableTagsForSite(ClientContext clientContext) { var web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); var tags = SPPolicyStoreProxy.GetAvailableTagsForSite(clientContext, clientContext.Url); return tags.Select(t => t.TagName); } } }
Sep 29 2017 01:59 AM
It appears that you must ExecuteQuery even though the ComplianceTag class is not a ClientObject. The SPPolicyStore in the OP still fails. It is not clear to me what the differences is between SPPolicyStore and SPPolicyStoreProxy.
private static IEnumerable<string> GetAvailableTagsForSite(ClientContext clientContext) { var web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); var tags = SPPolicyStoreProxy.GetAvailableTagsForSite(clientContext, clientContext.Url); clientContext.ExecuteQuery(); return tags.Select(t => t.TagName); }
Oct 16 2017 06:43 PM
Hi Gabriel,
I just read your post, did you manage to get a solution to access labels? I tried at my end as well but could not succeed.
Thanks
Rahul
Oct 16 2017 10:11 PM
Oct 17 2017 01:14 PM - edited Oct 17 2017 01:46 PM
Hi Gabriel,
Thanks for your response, my library is showing up the labels. But my requirement is how to get the label value that is applied to my site. For example if my site has been classified as highly confidential, then I need to generate a report which list down all the sites that are highly confidential.
The CSOM release shows a method (public method Microsoft.SharePoint.Client.CompliancePolicy.SPPolicyStoreProxy.GetAvailableTagsForSiteLabel). I tried using this method but getting the count as 0.
Oct 17 2017 01:32 PM
Oct 17 2017 02:35 PM
Hi Gabriel,
Thanks for your post, I was able to find my answer. A SharePoint site object has a property classification which provided the desired result in my case.
Apr 27 2021 01:32 AM