I have a piece of code in which I want to check if a specific Term is available within a TermSet. The TermSet is quite large, so getting all Terms is no option here.
My code looks like this:
Getting the correct TermSet:
using (ClientContext cc = _clientContext)
{
TaxonomySession session = TaxonomySession.GetTaxonomySession(_clientContext);
cc.Load(session, s => s.TermStores);
cc.ExecuteQuery();
TermStore termStore = session.TermStores.GetByName(TERM_STORE_NAME);
cc.Load(termStore, store => store.Groups
.Where(group => group.Name == TERM_GROUP_NAME)
.Include(group => group.TermSets
.Where(termSet => termSet.Name == TERM_SET_NAME)));
cc.ExecuteQuery();
_termSet = termStore.Groups[0].TermSets[0];
}
Checking if the Term exists within the TermSet:
public bool TermAvailableInTermSet(LabelMatchInformation labelparam)
{
LabelMatchInformation label = new LabelMatchInformation(_clientContext)
{
TrimUnavailable = false,
DefaultLabelOnly = false,
StringMatchOption = StringMatchOption.ExactMatch,
Lcid = 1033,
};
label.TermLabel = "Test";
TermCollection terms = LocationTermSet.GetTerms(label);
_clientContext.Load(label);
_clientContext.Load(terms);
_clientContext.ExecuteQuery();
return (terms != null && terms.Count > 0);
}
With this code everything runs like expected until I Execute the query with GetTerms
. A really peculiar error is returned: Value cannot be null.\r\nParameter name: labelMatchInformation.TermLabel
. So I obviously checked if I hadn't forgotten to set the TermLabel
variable. But the variable seemed fine:
What am I missing here? I assume this is the right way to use GetTerms, it also seems like the only option for me here.