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:
<Where> <In> <FieldRef LookupId='TRUE' Name='FieldName' /> <Values> <Value Type='Integer'>14</Value> <Value Type='Integer'>15</Value> </Values> </In></Where>
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 (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()));
}
}
}
Console.ReadLine();
}
Those Wss Id are that you need to put as the Value.
References
No comments:
Post a Comment