Thursday, November 17, 2011

Metadata term store management - Part-5


If you have not gone through part-1 to part-4 of this series, I would recommend you reading them first and then continue reading from here on.

In this post, we are going to talk about handling term store management programmatically. We will create term set and terms as well as navigate through the group inside term store session.

Before starting programming against term set, we need to take the term store session into a memory to work on it.

Here is a classic simple sample that helps you how to create a term set and add terms to it.

I have written a code in a button click event, in your case it can be at some different place based on your business logic.  Different actions are defined in the regions.

protected void btnAddTermStore_Click(Object sender, EventArgs e)
        {
            SPSite currentsite = SPContext.Current.Site;
          
                TaxonomySession session = new TaxonomySession(currentsite);

                if (session.TermStores.Count != 0)
                {
                    var termStore = session.TermStores["Managed Metadata Service"];

                    #region "Creating a group"

                    Group grpCloud = termStore.CreateGroup("Cloud Computing");
                    termStore.CommitAll();

                    #endregion

                     GroupCollection groupcoll = termStore.Groups;

                        foreach(Group grp in groupcoll)
                        {
                            if(grp.Name =="Sample Group")
                            {
                                 var termSet = grp.TermSets["Microsoft"];

                                 #region "Creating a term set"

                                 TermSet CTL = grp.CreateTermSet("Client Technologies");

                                 termStore.CommitAll();

                                 #endregion
                                                              

                                 #region "Adding Terms to an existing term set"

                                 Guid id = new Guid("{9CB722E2-D120-4A85-9740-447E01697CAC}");

                                 int lcid = CultureInfo.CurrentCulture.LCID;

                                 termSet.CreateTerm("TFS", lcid, id);

                                 termStore.CommitAll();

                                 #endregion
                            
                            }
                        }                
            }      
        }

All we have done here is we have taken the reference of taxonomy session of the site and then located term store management.


And this is what we get as a result



Once we have them in our hand, we created group. We then navigated to a group called Sample Group and located Microsoft Term Set. We also created a new term set called Client Technologies. We added new Term called TFS under Microsoft Term Set.

I hope this will give you an idea on basic manipulation of term store management.








1 comment:

Anonymous said...

Thanks it was very helpful.Bravo




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com