Skip to content

Instantly share code, notes, and snippets.

@gabbsmo
Created September 29, 2017 07:05
Show Gist options
  • Save gabbsmo/2bda7d93f20711cb39e2693a6066e1d5 to your computer and use it in GitHub Desktop.
Save gabbsmo/2bda7d93f20711cb39e2693a6066e1d5 to your computer and use it in GitHub Desktop.
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("[email protected]", 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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment