Friday, 27 May 2016

Add TermStore Managers and Contributors using CSOM

A new version of CSOM was released today for SharePoint Online. You can find more information about that here: http://dev.office.com/blogs/new-sharepoint-csom-version-released-for-Office-365-may-2016

This version adds the ability to add/retrieve Managers and Contributors to a Term Group in the SharePoint Online Term Store.

Here is a quick code snippet I put together for achieving this:

ClientContext clientContext = GetClientContext();
var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
var termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
var myTermGroup = termStore.Groups.GetByName("My Custom Terms Group");
//Add Group Managers
myTermGroup.AddGroupManager("i:0#.f|membership|ccdev2@tenant.onmicrosoft.com");
//Add Group Contributors
myTermGroup.AddContributor("i:0#.f|membership|ccdev1@tenant.onmicrosoft.com");
myTermGroup.AddContributor("i:0#.f|membership|ccdev3@tenant.onmicrosoft.com");
clientContext.Load(myTermGroup, group => group.GroupManagerPrincipalNames, group => group.ContributorPrincipalNames);
clientContext.ExecuteQuery();
Console.WriteLine("Group Managers: ");
foreach (var manager in myTermGroup.GroupManagerPrincipalNames)
{
Console.WriteLine(manager);
}
Console.WriteLine("Group Contributors: ");
foreach (var contributors in myTermGroup.ContributorPrincipalNames)
{
Console.WriteLine(contributors);
}

Once executed, you can see that the Term Group in the Term Store is updated:


No comments: