1

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 TermLabelvariable. But the variable seemed fine: Showing TermLabel has been filled 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.

2 Answers 2

0

Here is my sample test code which works in my local, hope it would help you.

using (var ctx = new ClientContext("http://sp:12001/"))
            {
                TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(ctx);
                //taxonomySession.UpdateCache();
                TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
                ctx.Load(termStore,
                    termStoreArg => termStoreArg.WorkingLanguage,
                    termStoreArg => termStoreArg.Id,
                    termStoreArg => termStoreArg.Groups.Include(
                        groupArg => groupArg.Id,
                        groupArg => groupArg.Name
                    )
                );
                ctx.ExecuteQuery();


                TermSet termSet = termStore.GetTermSet(new Guid("2bdf0a4e-9ff8-4db0-9f43-a9ea809d1b18"));
                ctx.Load(termSet);
                ctx.ExecuteQuery();
                //search for the Term to see if it exists
                LabelMatchInformation termQuery = new LabelMatchInformation(ctx)
                {
                    TermLabel = "Developer Members",                   
                    TrimUnavailable = false
                };
                var matchingTerms = termSet.GetTerms(termQuery);
                ctx.Load(matchingTerms);
                ctx.ExecuteQuery();

                if (matchingTerms.Count <1)
                {
                    Console.WriteLine("not found");
                }
                else
                {
                    Console.WriteLine(matchingTerms.Count);
                }

                Console.ReadKey();
            }
1
  • 1
    This code works, thanks a bunch. Could you explain to me though, what the crucial difference is here? (Compared to my example)
    – vonis22
    Sep 17, 2018 at 8:50
0

Two things.

  1. When you perform this query, you get that error message if you previously did changes to the termStore and did not explicitly call termStore.CommitAll(); ctx.ExecuteQuery();

  2. Docs state that only specific combinations of properties work:

https://learn.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-emmcsom/b6c29d86-6b37-417a-b3e7-9d1f026b9f6d

The LabelMatchInformation values that are set MUST be one of these combinations:

LabelMatchInformation.TermLabel and LabelMatchInformation.TrimUnavailable.

LabelMatchInformation.TermLabel, LabelMatchInformation.Lcid, and LabelMatchInformation.TrimUnavailable.

LabelMatchInformation.TermLabel, LabelMatchInformation.DefaultLabelOnly, LabelMatchInformation.StringMatchOption, LabelMatchInformation.ResultCollectionSize, and LabelMatchInformation.TrimUnavailable.

LabelMatchInformation.TermLabel, LabelMatchInformation.Lcid, LabelMatchInformation.DefaultLabelOnly, LabelMatchInformation.StringMatchOption, LabelMatchInformation.ResultCollectionSize, and LabelMatchInformation.TrimUnavailable.

If the combination of LabelMatchInformation (section 3.1.5.30) values is invalid, an ArgumentException or ArgumentNullException will be thrown indicating the invalid setting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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