Forum Discussion

Gabriel Smoljar's avatar
Gabriel Smoljar
Brass Contributor
Sep 29, 2017

How to get available classification labels with CSOM?

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 https://dev.office.com/blogs/new-sharepoint-csom-version-released-for-Office-365-august-2017 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);
    }
    • Rahul Srivastava's avatar
      Rahul Srivastava
      Copper Contributor

      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

      • Gabriel Smoljar's avatar
        Gabriel Smoljar
        Brass Contributor
        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.

Resources