SharePoint 2013 CSOM Taxonomy (Managed Metadata)
SharePoint 2013 has introduced Microsoft.SharePoint.Client.Taxonomy.dll. It means that we can do play with Taxonomy using client side object model instead of just relying on server side code. This dll is located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\. Programming using CSOM is almost similar to server side object model.
The below code shows how to create Termsets, Groups, Terms.
publicclassSharePointWebContext : IDisposable
{
public Microsoft.SharePoint.Client.ClientContext SharePointClientContext = null;
public Microsoft.SharePoint.Client.Web Web = null;
publicstring Url = string.Empty;
protectedILog Log = LogManager.GetLogger(typeof(SharePointWebContext));
public SharePointWebContext(Microsoft.SharePoint.Client.Web web, string url)
{
this.Web = web;
this.Url = url;
}
public SharePointWebContext(string url)
{
this.Url = url;
this.SharePointClientContext = newClientContext(this.Url);
this.Web = this.SharePointClientContext.Web;
}
#region IDisposable Members
///<summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
/// resources.
///</summary>
voidIDisposable.Dispose()
{
}
#endregion
}
[Description("Processes an XML string containing managed metadata terms, termsets and groups.")]
public class WebCreateTermsets
{
public WebCreateTermsets(Microsoft.SharePoint.Client.ClientContext context)
: base(context)
{ }
[Parameter(1, typeof(String), "SiteUrl", "The web url")]
[Parameter(2, typeof(String), "TermSetXmlData", "An XML string containing terms, termsets and groups")]
protectedoverridevoid OnExecute(paramsobject[] args)
{
try
{
string webUrl = GetParameter<string>(args, 1);
string xmlData = GetParameter<string>(args, 2);
TermStoreInfo termStoreInfo = TermStoreInfo.FromXml(xmlData);
using (var context = new SharePointWebContext(webUrl))
{
Microsoft.SharePoint.Client.Web web = context.Web;
TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(web.Context);
TermStore termStore = taxonomySession.TermStores.GetByName(termStoreInfo.Name);
web.Context.Load(termStore);
web.Context.ExecuteQuery();
if (termStore == null)
{
Log.ErrorFormat("The Taxonomy Service is offline or unavailable.");
return;
}
CreateGroups(termStoreInfo, termStore, web);
}
}
catch (Exception x)
{
Log.Exception(x);
}
}
protectedvoid CreateGroups(TermStoreInfo termstoreInfo, TermStore termStore, Microsoft.SharePoint.Client.Web web)
{
if (termstoreInfo != null && termstoreInfo.Groups.Count > 0)
{
foreach (GroupInfo groupInfo in termstoreInfo.Groups)
{
TermGroup termGroup = default(TermGroup);
try
{
termGroup = termStore.Groups.GetByName(groupInfo.Name);
web.Context.Load(termGroup);
web.Context.ExecuteQuery();
}
catch (Exception)
{
termGroup = null;
}
if (termGroup == null)
{
termGroup = termStore.CreateGroup(groupInfo.Name, Guid.NewGuid());
termStore.CommitAll();
web.Context.ExecuteQuery();
}
CreateTermsets(groupInfo, termGroup, termStore, web);
}
}
}
protectedvoid CreateTermsets(GroupInfo groupInfo, TermGroup termGroup, TermStore termStore, Microsoft.SharePoint.Client.Web web)
{
foreach (TermSetInfo termsetInfo in groupInfo.TermSets)
{
TermSet termSet = default(TermSet);
try
{
termSet = termGroup.TermSets.GetByName(termsetInfo.Name);
web.Context.Load(termSet);
web.Context.ExecuteQuery();
}
catch (Exception)
{
termSet = null;
}
if (termSet == null)
{
termSet = termGroup.CreateTermSet(termsetInfo.Name, Guid.NewGuid(), 1033);
termStore.CommitAll();
web.Context.ExecuteQuery();
}
CreateTerms(termsetInfo, termSet, termStore, web);
}
}
protectedvoid CreateTerms(TermSetInfo termSetInfo, TermSet termSet, TermStore termStore, Microsoft.SharePoint.Client.Web web)
{
foreach (TermInfo termInfo in termSetInfo.Terms)
{
Term term = default(Term);
try
{
term = termSet.Terms.GetByName(termInfo.Name);
web.Context.Load(term);
web.Context.ExecuteQuery();
}
catch (Exception)
{
term = null;
}
if (term == null)
{
term = termSet.CreateTerm(termInfo.Name, 1033, Guid.NewGuid());
termStore.CommitAll();
web.Context.ExecuteQuery();
}
CreateTerms(termInfo, term, termStore, web);
}
}
protectedvoid CreateTerms(TermInfo termInfo, Term term, TermStore termStore, Microsoft.SharePoint.Client.Web web)
{
foreach (TermInfo childTermInfo in termInfo.ChildTerms)
{
Term childTerm = default(Term);
try
{
childTerm = term.Terms.GetByName(childTermInfo.Name);
web.Context.Load(childTerm);
web.Context.ExecuteQuery();
}
catch (Exception)
{
childTerm = null;
}
if (childTerm == null)
{
childTerm = term.CreateTerm(childTermInfo.Name, 1033, Guid.NewGuid());
termStore.CommitAll();
web.Context.ExecuteQuery();
}
CreateTerms(childTermInfo, childTerm, termStore, web);
}
}
protected T GetParameter<T>(object[] args, int parameterId)
{
if (args == null || parameterId > args.Length)
returndefault(T);
return (T)args[parameterId - 1];
}
}
The input for this class is going to be in below format. The input is encoded xml.
<SharePointCommands xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns="https://schemas.sharepointarchitects.com/caml.net/SharePointCommandSet.xsd">
<Command Type="WebCreateTermsets">
<Name>Caml.Net.SharePoint.Tools.Web.WebCreateTermsets</Name>
<Description>Processes an XML string containing managed metadata terms, termsets and groups.</Description>
<Parameters>
<Parameter>
<Name>SiteUrl</Name>
<Type>System.String</Type>
<Description>SiteUrl</Description>
<Value>https://win-64t2uiim6ta/sites/TestBox</Value>
</Parameter>
<Parameter>
<Name>TermSetXmlData</Name>
<Type>System.String</Type>
<Description>TermSetXmlData</Description>
<Value>
<TermStore>
<Name>Managed Metadata Service</Name>
<Groups>
<Group>
<Name>Groupl</Name>
<Termsets>
<Termset>
<Name>Termset1</Name>
<Terms>
<Term>
<Name>Level1Term</Name>
<Terms>
<Term>
<Name>Level11Term</Name>
</Term>
<Term>
<Name>Level12Term</Name>
</Term>
</Terms>
</Term>
<Term>
<Name>Level2Term</Name>
</Term>
</Terms>
</Termset>
<Termset>
<Name>Termset2</Name>
<Terms>
<Term>
<Name>Level1Term</Name>
<Terms>
<Term>
<Name>Level11Term</Name>
</Term>
<Term>
<Name>Level12Term</Name>
</Term>
<Term>
<Name>Level13Term</Name>
</Term>
</Terms>
</Term>
<Term>
<Name>Level2Term</Name>
</Term>
<Term>
<Name>Level3Term</Name>
</Term>
</Terms>
</Termset>
</Termsets>
</Group>
<Group>
<Name>Group2</Name>
<Termsets>
<Termset>
<Name>Termset1</Name>
<Terms>
<Term>
<Name>Level1Term</Name>
<Terms>
<Term>
<Name>Level11Term</Name>
</Term>
<Term>
<Name>Level12Term</Name>
</Term>
</Terms>
</Term>
<Term>
<Name>Level2Term</Name>
</Term>
</Terms>
</Termset>
<Termset>
<Name>Termset2</Name>
<Terms>
<Term>
<Name>Level1Term</Name>
<Terms>
<Term>
<Name>Level11Term</Name>
</Term>
<Term>
<Name>Level12Term</Name>
</Term>
<Term>
<Name>Level12Term</Name>
</Term>
</Terms>
</Term>
<Term>
<Name>Level2Term</Name>
</Term>
<Term>
<Name>Level3Term</Name>
</Term>
</Terms>
</Termset>
</Termsets>
</Group>
</Groups>
</TermStore>
</Value>
</Parameter>
</Parameters>
</Command>
</SharePointCommands>
Here is the output:
Comments
- Anonymous
December 08, 2013
Hi Guru Pratap Ketepalli, Could you please indicate where the 'TermStoreInfo' class is defined? I'm not able to find it in the MSDN documentation. Thanks for your good work!