Thursday, October 6, 2011

How to create Terms and Term Set programmatically (using code)?

How to create Terms and Term Set programmatically?

Add reference of "Microsoft.SharePoint.Taxonomy" assembly to your project. Include namespace "Microsoft.SharePoint.Taxonomy". And use the code as listed below.


 using (SPSite site = new SPSite("http://anmol-pc"))
            {
                TaxonomySession _TaxonomySession = new TaxonomySession(site);

                //Get instance of the Term Store 
                TermStore _TermStore = _TaxonomySession.TermStores["My Term Store"];

                //Now create a new Term Group
                Group _Group = _TermStore.CreateGroup("My New Group");

                //Create a new Term Set in the new Group
                TermSet _TermSet = _Group.CreateTermSet("My New Termset");

                //Add terms to the term set
                Term _term1 = _TermSet.CreateTerm("First Term", 1033);
                Term _term2 = _TermSet.CreateTerm("Second Term", 1033);
                Term _term3 = _TermSet.CreateTerm("Third Term", 1033);
                Term _term4 = _TermSet.CreateTerm("Last Term", 1033);

                //commit changes
                _TermStore.CommitAll();

            }

1 comment:

  1. Thanks :), this is helpful :), is there a way to accomplish this in sandboxed solutions?, if taxonomy can be used in a site collection, there should be a way of creating groups and terms... :(

    ReplyDelete

Popular Posts