I had the occasion to need to figure out accessing the Managed Metadata Store using the new client object model libraries for SharePoint 2013. In snooping around, I found virtually no documentation on this topic whatsoever, other than generic boilerplate class definitions that have been clearly emitted by some automated process rather than a human who's actually done any development with it. So, for those of you looking around the Microsoft.SharePoint.Client.Taxonomy namespace, hopefully this will provide a little kick start to get you headed in the right direction.
The first thing to understand is that, much like the managed code object model, the TaxonomySession is the one that rules them all. Unfortunately if you look at the fairly arcane MSDN docs on this topic it suggests that there is some usable constructor for this class. Please look at the dancing monkey in my left hand and ignore that documentation - it is poo. Instead the way to get started with the TaxonomySession is to create your ClientContext instance first, and then use that to create a new TaxonomySession. For purposes of this discussion I will skip showing how to create your ClientContext instance because there are a ton of examples out there about how to do that; in my case I've written a simple SharePoint App and am using the new model to do the lifting for me there to get my ClientContext. Once I have that I can get my TaxonomySession like so ("CheckClientContext() is the method I use to get my ClientContext variable, ctx, initialized):
TaxonomySession ts = TaxonomySession.GetTaxonomySession(ctx);
So at this point I haven't really done anything...there's no actual data in my TaxonomySession that I can use, but it's at least set up for use. At this point I can now start loading up the collections of data that make up most of the elements in the Managed Metadata Service. The key thing, like all collections you access via CSOM, is that you need to Load() them with the ClientContext before you try and access any of their members, otherwise you will error out. So here's an example of starting to drill down to get the collection of Term Stores (of which there is usually just one, but you get the point - illustrates how to use the collections):
TermStoreCollection tsc = ts.TermStores;
foreach (TermStore tStore in tsc)
//do something here
From here you should be off and running at least as far as the pattern is concerned for using this. What I'll do here is paste some code that I use to enumerate through all the goo in my Managed Metadata Service, down to all of the individual terms. Not a production app of course, but hopefully a good illustration of how to work your way through the model. I mean "illustration" figuratively, since the formatting on this site continues to suck eggs:
System.Text.StringBuilder sb = new System.Text.StringBuilder(4096);
//before referring to any member, need to execute query, i.e.
//TermStore tStore = tsc; or foreach...
sb.Append("Term Store: " + tStore.Name + Environment.NewLine);
foreach (TermGroup tg in tStore.Groups)
sb.Append("\t->" + "Term Group: " + tg.Name + Environment.NewLine);
foreach (TermSet tSet in tg.TermSets)
sb.Append("\t\t->" + "Term Set: " + tSet.Name + Environment.NewLine);
foreach (Term t in tSet.Terms)
sb.Append("\t\t\t->" + "Term: " + t.Name + Environment.NewLine);
Here you can see an example of the output from my farm:
This should be enough to get you anywhere you need to in the Taxonomy client model. Enjoy!
Great post - really good overview of taxonomy use - thanks.
Also there is another post at
How to establish taxonomy session using powershell?
MSDN should just use this blog as documentation.