Using Taxonomy AKA Managed Metadata AKA TermSets with CSOM in SharePoint 2013


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):

 

CheckClientContext();

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):

ctx.Load(ts.TermStores);

TermStoreCollection tsc = ts.TermStores;

ctx.ExecuteQuery();

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:

 

CheckClientContext();

TaxonomySession ts = TaxonomySession.GetTaxonomySession(ctx);

ctx.Load(ts.TermStores);

TermStoreCollection tsc = ts.TermStores;

 

System.Text.StringBuilder sb = new System.Text.StringBuilder(4096);

 

//before referring to any member, need to execute query, i.e.

//TermStore tStore = tsc[0]; or foreach…

ctx.ExecuteQuery();

foreach (TermStore tStore in tsc)

{

    sb.Append("Term Store: " + tStore.Name + Environment.NewLine);

    ctx.Load(tStore.Groups);

    ctx.ExecuteQuery();

                   

     foreach (TermGroup tg in tStore.Groups)

    {

        sb.Append("\t->" + "Term Group: " + tg.Name + Environment.NewLine);

        ctx.Load(tg.TermSets);

        ctx.ExecuteQuery();

      foreach (TermSet tSet in tg.TermSets)

            {

                sb.Append("\t\t->" + "Term Set: " + tSet.Name + Environment.NewLine);

                ctx.Load(tSet.Terms);

                ctx.ExecuteQuery();

                     foreach (Term t in tSet.Terms)

                {

                    sb.Append("\t\t\t->" + "Term: " + t.Name + Environment.NewLine);

                }

            }

        }

    }

Debug.WriteLine(sb.ToString());

 

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!

 

Comments (5)

  1. alexandrad9x says:

    Tao http://dichvuketoanlongbien.com/
    Rủa
    http://dichvuketoanlongbien.com/a2-96-dich-vu-ke-toan-tron-goi.html
    Thằng http://dichvuketoanlongbien.com/a2-98-dich-vu-ke-toan-thue.html
    Cờ
    http://dichvuketoanlongbien.com/a2-103-dich-vu-bao-cao-tai-chinh.html
    http://dichvuketoanlongbien.com/a2-97-dich-vu-quyet-toan-thue.html
    Nào
    http://dichvuketoanlongbien.com/a2-114-dich-vu-ke-toan-tai-29-quan-huyen.html
    Soi
    http://dichvuketoanlongbien.com/i780-dich-vu-ke-toan-thue-tron-goi-tai-bac-ninh.html
    Tài
    http://dichvuketoanlongbien.com/i779-dich-vu-ke-toan-thue-tron-goi-tai-bac-giang.html
    Khoản
    http://dichvuketoanlongbien.com/i778-dich-vu-ke-toan-thue-tron-goi-tai-phu-tho.html

    http://dichvuketoanlongbien.com/i781-dich-vu-ke-toan-thue-tron-goi-tai-hung-yen.html
    Link
    http://dichvuketoanlongbien.com/i782-dich-vu-ke-toan-thue-tron-goi-tai-vinh-phuc.html
    Của
    http://dichvuketoanlongbien.com/i783-dich-vu-ke-toan-thue-tron-goi-tai-hai-phong.html
    Tao. http://www.trungtamketoan.com.vn/
    Chúng
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-ha-noi.html
    Mày
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-tp-hcm.html
    Đủ
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-quang-ninh.html
    Trình
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-hai-duong.html
    Thì
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-bac-giang.html
    Tự
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-bac-ninh.html
    Đi
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-hai-phong.html

    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-nam-dinh.html
    Làm.
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-thai-binh.html
    Việc
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-thanh-hoa.html

    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-vinh-phuc.html
    Phải
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-hung-yen.html
    Rẻ
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-phu-tho.html
    Rách
    http://www.trungtamketoan.com.vn/p/trung-tam-dao-tao-ke-toan-tai-binh-duong.html
    Như http://www.tosvn.com
    Thế. http://iketoan247.blogspot.com
    Loại http://tailieuveketoan.blogspot.com
    Chó http://mauhinhnendep.blogspot.com
    Má. http://www.tosvn.com/search/label/Hack%20CF
    Tao http://www.tosvn.com/search/label/Hack%20AvatarStar
    Rủa http://www.tosvn.com/search/label/Hack%20Warcraft-Dota2
    Những http://hocketoan360.com/category/tai-lieu-ke-toan/
    Thằng http://iketoan247.blogspot.com/search/label/thong-tin-kinh-te
    Soi http://iketoan247.blogspot.com/search/label/tin-bai-ve-thue
    Tao http://hoclamketoan.edu.vn/
    Sẽ http://hoclamketoan.edu.vn/category/khoa-hoc-ke-toan
    Tan http://hoclamketoan.edu.vn/category/dich-vu-ke-toan
    Cửa http://hoclamketoan.edu.vn/category/hoc-lam-ke-toan
    Nát http://hoclamketoan.edu.vn/category/tai-lieu-ke-toan
    Nhà http://hocketoan360.com/
    Haha http://hocketoan360.com/category/khoa-hoc-ke-toan/
    http://hocketoan360.com/category/dich-vu-ke-toan/

  2. Alex Talarico says:

    Great post – really good overview of taxonomy use – thanks.

  3. jim says:

    How to establish taxonomy session using powershell?

  4. Cam says:

    MSDN should just use this blog as documentation.

Skip to main content