All the Terms or Managed Metadata or Enterprise Keywords are stored in a list named "TaxonomyHiddenList". By querying on the list, we can get our desired terms. The method is illustrated below:
private List<string> getTerms(string termSetId)
{
var terms = new List<string>();
try
{
var spContext = GetSharepointContext();
using (ClientContext context = spContext.CreateUserClientContextForSPHost())
{
List list = context.Web.Lists.GetByTitle("TaxonomyHiddenList");
var camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View><Query><Where><Eq>
<FieldRef Name='IdForTermSet'/><Value Type='Text'>"
+ termSetId + "</Value></Eq></Where></Query></View>";
Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(camlQuery);
context.Load(listItems);
context.ExecuteQuery();
foreach (var term in listItems.ToList())
{
var termName = term.FieldValues["Title"].ToString();
if (terms.Contains(termName) == false)
terms.Add(termName);
}
}
}
catch (Exception ex)
{
throw ex;
}
return terms;
}
No comments:
Post a Comment