Question

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.

Was it helpful?

Solution

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();
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top