How to Iterate the Terms using Termset Id in sharepoint 2013?
-
11-12-2020 - |
Pregunta
I need to Iterate the terms using Termset ID, I Tried
TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termstore = taxonomySession.DefaultSiteCollectionTermStore;
TermSet breadcrumbTermSet = termstore.GetTermSet(new Guid("fdc98309-c520-44eb-a5ca-f2a04d8dd541"));
var terms = breadcrumbTermSet.Terms;
var siteCollectionURL = SPContext.Current.Web.Url;
var requestURL = HttpContext.Current.Request.Url;
string termSetName = "", termName = "";
foreach (var parentTerm in terms)
{
if (parentTerm.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"] == siteCollectionURL.ToString())
{
termSetName = " /  " + parentTerm.Name;
}
foreach (var childTerm in parentTerm.Terms)
{
if (parentTerm.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"] == requestURL.ToString())
{
termSetName = " /  " + childTerm.Name;
}
}
}
But I can't, I need more Reference about this, can you please suggest some Idea to achieve this
Solución
In C# Server object model, you can try it as below:
TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termstore = taxonomySession.DefaultSiteCollectionTermStore;
Guid termSetGuid = new Guid("fdc98309-c520-44eb-a5ca-f2a04d8dd541");
TermSet breadcrumbTermSet = termstore.GetTermSet(termSetGuid);
TermCollection terms = breadcrumbTermSet.GetAllTerms();
var siteCollectionURL = SPContext.Current.Web.Url;
var requestURL = HttpContext.Current.Request.Url;
string termSetName = "", termName = "";
foreach (var parentTerm in terms)
{
if (parentTerm.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"] == siteCollectionURL.ToString())
{
termSetName = " /  " + parentTerm.Name;
}
foreach (var childTerm in parentTerm.Terms)
{
if (parentTerm.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"] == requestURL.ToString())
{
termSetName = " /  " + childTerm.Name;
}
}
}
Otros consejos
There are couple options you can try.
- You can call
TermSet.GetAllTerms()
method to return all terms under that TermSet. - Another approach you can try is to use recursive function to enumerate
termSet.Terms
andterm.Terms
I list the code for approach 1 at the following. You can check term.PathOfTerm
to find out the term's hierarchy information.
class Program
{
static void Main(string[] args)
{
Test();
}
private static void Test()
{
string url = "http://sp1:1111";
using (ClientContext context = new ClientContext(url))
{
TaxonomySession session = TaxonomySession.GetTaxonomySession(context);
TermStore store = session.GetDefaultSiteCollectionTermStore();
TermSet termSet = store.GetTermSet(new Guid("6e17ec6e-7456-4e61-82e9-d5a75f6932fa"));
TermCollection terms = termSet.GetAllTerms();
context.Load(terms);
context.ExecuteQuery();
foreach(Term term in terms)
{
Console.WriteLine(term.PathOfTerm);
}
Console.WriteLine();
}
}
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a sharepoint.stackexchange