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