How to get available classification labels with CSOM?

Brass Contributor

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);
        }
    }
}
7 Replies

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);
}

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

See the code in my first reply in this thread. That code was working for me. Keep in mind that it can take up to 24 hours for labels to be propagated to a site. Check in library settings before testing your code just to be sure.

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.

Then I am afraid I do not know. I was trying to get the classification label from individual documents. I suggest you start a new thread.

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.

Hi Rahul, Can you please tell what setting you did to make it working?