none
How to Verify if given term exist in TermSet group/sub group using server side object model for console app? RRS feed

  • Question

  • My code is not working to verify in sub level of term set.please provide the code only in server side object model to verify whether the given name exist or not?

    My sample code is given below : 

     Term curTerm = null;
                                                                TermSet locationTermSet = group.TermSets["Location"];
                                                                string termValue = person.Element(item.Value).Value;// from xml file i am reading
                                                                if (!string.IsNullOrEmpty(termValue))
                                                                {
                                                                    if (!locationTermSet.Terms.Any(t => t.Name == termValue))
                                                                    {
                                                                        curTerm = locationTermSet.CreateTerm(termValue, 1033);
                                                                    }
                                                                    termStore.CommitAll();
                                                                    curTerm = locationTermSet.Terms.Where(t => t.Name == termValue).FirstOrDefault();
                                                                    
                                                                }

    Thursday, January 3, 2019 12:13 PM

All replies

  • Here are the links below

    https://sharepoint.stackexchange.com/questions/111321/how-do-you-check-if-a-term-already-exists-in-a-term-store

    https://social.technet.microsoft.com/Forums/en-US/680987e1-1efd-49d3-a3b4-89e8d41218b6/check-if-a-taxonomy-group-exists-by-name-in-c?forum=sharepointdevelopment


    Please remember to click Mark as Answer on the answer if it helps you

    Thursday, January 3, 2019 8:22 PM
  • 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();
    
            
        
        
    }


    Please remember to click Mark as Answer on the answer if it helps you

    Thursday, January 3, 2019 8:24 PM
  • Hi Sudhir,

    Here is a code sample to check if a term exist in Term Set Group, please check:

    SPSite site = new SPSite("http://projsrv2016:43585/sites/dev");
                TaxonomySession taxonomySession = new TaxonomySession(site);
                TermStore termStore = taxonomySession.TermStores[0];
                Group group = termStore.Groups["Test"];
                if (group!=null)
                {
                    TermSet locationTermSet = group.TermSets["Term1"];
                    if (locationTermSet != null)
                    {
                        string termValue = "TermSub3";
                        if (!string.IsNullOrEmpty(termValue))
                        {
                            if (!locationTermSet.Terms.Any(t => t.Name == termValue))
                            {
                                locationTermSet.CreateTerm(termValue, 1033);
                            }
                            termStore.CommitAll();
    
    
    
                        }
                    }
                    
                }

    Use TermCollection.Any function will be the right solution based on my test.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Friday, January 4, 2019 8:23 AM