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:-
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 ?