none
Check if a taxonomy Group exists by name in C#? RRS feed

  • Question

  • 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.

    Monday, September 7, 2015 6:09 PM

Answers

  • The server object model does not support accessing something by its name. The indexer for the Groups collection on the server side takes a guid of the group. If you want to check by name then you must use the client object model. The code below is a possible example.

    public void DoesTermGroupExist()
        {
    
            ClientContext ctx = new ClientContext("http://basesmc15");
    
            TaxonomySession ts = TaxonomySession.GetTaxonomySession(ctx);
    
            TermStore store = ts.TermStores[0];
    
            TermGroup termGroup = store.Groups.GetByName("People");
    
            ctx.Load(store);
            ctx.Load(termGroup);
    
            ctx.ExecuteQuery();
    
            
        
        
    }


    Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer

    Tuesday, September 8, 2015 1:45 AM
  • Hi,

    Microsoft.SharePoint.Taxonomy.GroupCollection implements IEnumerable<Group>, so we can use LINQ to check it.

    if(termStore.Groups.Any(x => x.Name == "MYT"))
    {
       // group contains at least one item matching the predicate.
    }
    else
    {
       // group contains no items matching the predicate.
    }

    Best Regards,

    Dennis


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Wednesday, September 9, 2015 7:28 AM
    Moderator

All replies

  • The server object model does not support accessing something by its name. The indexer for the Groups collection on the server side takes a guid of the group. If you want to check by name then you must use the client object model. The code below is a possible example.

    public void DoesTermGroupExist()
        {
    
            ClientContext ctx = new ClientContext("http://basesmc15");
    
            TaxonomySession ts = TaxonomySession.GetTaxonomySession(ctx);
    
            TermStore store = ts.TermStores[0];
    
            TermGroup termGroup = store.Groups.GetByName("People");
    
            ctx.Load(store);
            ctx.Load(termGroup);
    
            ctx.ExecuteQuery();
    
            
        
        
    }


    Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer

    Tuesday, September 8, 2015 1:45 AM
  • Hi,

    Microsoft.SharePoint.Taxonomy.GroupCollection implements IEnumerable<Group>, so we can use LINQ to check it.

    if(termStore.Groups.Any(x => x.Name == "MYT"))
    {
       // group contains at least one item matching the predicate.
    }
    else
    {
       // group contains no items matching the predicate.
    }

    Best Regards,

    Dennis


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Wednesday, September 9, 2015 7:28 AM
    Moderator