2

I am developing an ItemUpdated event receiver inside my on-premises farm 2013. and inside my event receiver i want to get all the term sets under a term group. so i define the following :-

using (SPSite site = new SPSite(properties.SiteId))
       {
             string currenweburl = properties.RelativeWebUrl;
             using (SPWeb spCurrentSite = site.OpenWeb(currenweburl))
                     {

                       Term currenttermset = null;
                       TaxonomySession txSession = new TaxonomySession(site);
                       TermStore tc = txSession.TermStores[termstorename.Trim()];
                       Group g = tc.Groups[groupname.Trim()];
                       TermSet ts = g.TermSets[termsetname.Trim()];

now getting the TaxonomySession + TermStore + Group always work fine. But when it comes to the following code TermSet ts = g.TermSets[termsetname.Trim()]; i keeping getting the following exception:-

Specified argument was out of the range of valid values.

and when i check the term store count when debugging the code i found that the count will be equal to 0. while i already have one term set which is used to store the navigation terms. here is the count inside Visual Studio:-

enter image description here

also in one case i added a new term group, then i add a new term set, and i use this term set for site navigation. now i was able to retrieve the term set correctly inside the event receiver. but when i added another term set under the same group, the code raised the above exception, and the term set count will be equal to 0 again while debugging the code. so not sure how i can get all the term sets under my term group ? same as the term sets which will be shown inside the "Site Settings" >> "Term Store Management" page?? in all ways not sure why my code was not able to achieve this ?

  • Check the permission. you can see an "System.UnauthorizedAccessException" at (e.g.) the Contributors of the group object. Maybe you need to run your code in ElevatedPrivileges – ReTech Oct 2 '17 at 11:34
  • @ReTech i am already running the code with elevated privileges. – john Gu Oct 3 '17 at 14:07
  • 1
    did you create the SPSite-Object (used in the TaxonomySession) in ElevatedPriveleges too? If not, it is still in the lower privileges, i guess. the above code shows that you create this object, but better look twice :) – ReTech Oct 4 '17 at 8:05
3

Please try this once.

private static TermSet RetrunTermSetIfExists(ClientContext clientContext, TermGroup termGroup, string termSetName)
    {
        clientContext.Load(termGroup.TermSets);
        clientContext.ExecuteQuery();
        TermSet termset = termGroup.TermSets.FirstOrDefault(x => x.Name.ToLower() == termSetName.ToLower());
        if (termset == null)
        {
            return null;
        }
        else
        {
            clientContext.Load(termset);
            clientContext.ExecuteQuery();
            return termset;
        }
    }

Sorry. The above code is for CSOM. But it works perfectly.

1

Your navigation term set has "Available for tagging" unchecked. Term sets marked as such will not be found in code. You need to make sure "available for tagging" is checked on the term set.

  • thanks for the help.. now when i set the term set as "Available for tagging" it start showing inside the code.. but still i am totally confused, as i am usually able to get the term sets even if they are not defined for tagging. for example in a previous case , i created a new enterprise wiki site collection, and it comes with a built-in navigation term set which is not set as "Available for tagging" , and i am able to retrive it inside the code as is.. but now in my case i have created a new term set for a wiki sub-site, – john Gu Oct 5 '17 at 23:53
  • ... and i define it to be used for managed navigation, and seems this manually created term set will not be available inside the code unless i set it as "Available for tagging" .... so not sure what is going on ? why the built-in term set which comes with a new enterprise wiki site collection will be available inside the code even if it is not set as "Available for tagging",, while the manually created term set inside the enterprise wiki sub site will not be available inside the code unless i set it as "Available for tagging"?? really strange ! – john Gu Oct 5 '17 at 23:56
  • do you have any idea what is causing this confusion as mentioned inside my above 2 comments ? – john Gu Oct 6 '17 at 9:46
  • I can't say what's going on in your environment, but in my experience, term sets set as not available for tagging will not be returned in code. Good luck! – Derek Gusoff Oct 6 '17 at 15:13

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.