2

Lets say I have a whole lot of Terms in the Term Store.

When I create the Term items I am setting a custom property like this:

Term term = termSet.CreateTerm("ParentTerm", 1029, new Guid("{0368F902-740B-4855-A4A7-FA8F96813CCB}"));
term.SetCustomProperty("OBJID", "2222"); 
termStore.CommitAll();

So far so good, but now how would I perform a search to find this particular term based on its custom property, in this case OBJID.

Is there built in search methods for terms?

There is the

TaxonomySession session = new TaxonomySession(site);
session.GetTerm("GUID_OF_TERM");

But this is not the same as the term custom property - thanks!

1

Check if this exists:

term.CustomProperties["key"] 

Or if you want to check all of the Term Set:

termSet.Terms.Select(x => x.CustomProperties["key"] != null);

This could be made into a recursive function to check for all child terms.

1

There is method called GetTermsWithCustomProperty in TaxonomySession and TermSet classes. Probably you can use that one.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.termstore.gettermswithcustomproperty.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomysession.gettermswithcustomproperty.aspx

0

There is no out of the box functionality to search for this. You have to build some kind of recursive function to do this on your own - ultimatley you need to traverse all Terms, which likely would kill performance.

I would suggest you rethink your design.

0

Try modifying the below code. I use it to recursively find the first Tag that matches a Term label. You would have to change the if statement to query for your custom property.

private Term GetChildTermThatMatchesName(Term sourceTerm, string name)
{

   Term  term = null;

   if (sourceTerm != null)
   {
      if (sourceTerm.TermsCount > 0)
      {
         if (sourceTerm.Terms.Any(t => t.Name == TaxonomyItem.NormalizeName(name)))
         {
            term =  sourceTerm.Terms.First(t => t.Name == TaxonomyItem.NormalizeName(name));
         }
         else
         {
            /* Recusively loop through child until we find our first match */
            foreach (Term childTerm in sourceTerm.Terms)
            {
               if (term == null)
               {
                  term = GetChildTermThatMatchesName(childTerm, name);
               }
               else
               {
                  break;
               }
            }
         }
      }
   }


   return term;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.