0

I am using the Managed Metadata Service and I want to check if a group under a specific TermStore already exists.

I tried the following code but I get an error (see below):

string siteCollectionUrl = "http://sp2013/sites/mytax";
SPSite site = new SPSite(siteCollectionUrl);
SPWeb web = site.OpenWeb();

TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termStore = taxonomySession.TermStores[0];

Group termGroup = termStore.Groups["MYT"]

if (termGroup != null)
{
    ...

At the following line the error occurs:

Group termGroup = termStore.Groups["MYT"]

The error message is:

Specified argument was out of the range of valid values. Parameter name: index

I want check if the group exist and if not, I want to create the group.

0

You most probably want to use taxonomySession.DefaultKeywordsTermStore instead of taxonomySession.TermStores[0]

This is how you check if a group exists:

if (!termStore.Groups.Any(g => g.Name == "MYT"))
{
   termStore.CreateGroup("MYT");
   //you can specify a GUID if needed: 
   //termStore.CreateGroup("MYT", new Guid("{46D4495B-37B5-4313-B8B9-43F7E99E3028}"));
}
  • 1
    And thanks a lot for the hint on "taxonomySession.DefaultKeywordsTermStore" – BOG Sep 8 '15 at 8:32

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.