Query Managed Metadata Column in SharePoint List / Library


Following my previous article to Populate Managed Metadata Column, In this article I would like to show you on How to Query Managed Metadata column in SharePoint List / Library.

If you look at the msdn article, the query would look like below:

  <FieldRef LookupId='TRUE' Name='FieldName' />
   <Value Type='Integer'>14</Value>
   <Value Type='Integer'>15</Value>


The question is where do we get that Values in the query ?

To get those values, I’ve created a SharePoint console application.

static void Main(string[] args)
   using(SPSite site = new SPSite("http://yoursiteurl"))
      using (SPWeb web = site.OpenWeb())
         SPList list = web.Lists.TryGetList("MyList");
         TaxonomyField audienceType = (TaxonomyField)site.RootWeb.Fields.GetField("MyManagedColumn");
         TaxonomySession taxonomySession = new TaxonomySession(site);
         TermStore termStore = taxonomySession.TermStores[audienceType.SspId];
         TermSet termSet = termStore.GetTermSet(audienceType.TermSetId);

         TermCollection termColl = termSet.GetAllTerms();
         foreach (Term eachTerm in termColl)
            int[] wssIds = TaxonomyField.GetWssIdsOfTerm(site, termStore.Id, termSet.Id, eachTerm.Id, false, 500);
            if(wssIds.Length > 0)
               Console.WriteLine(string.Format("Term: {0}, WssId: {1}", eachTerm.Name.Trim(), wssIds[0].ToString()));

Those Wss Id are that you need to put as the Value.



Tagged: , , , , , , , , , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: