Managed Metadata is a new feature introduced in
SharePoint 2010. It allows you to define terms in a hierarchical collection
centrally, and create a managed metadata field that is reference a term sets.
By using the Term Store Management Tool found in Managed
Metadata Service, you can create a term set either manually, or by importing a
CSV file. You can also create a term set programmatically, either by a feature
receiver or a console application.
Now the below code will let you to create a new Term in
Keywordstermset and this term can be added as Tag to the Page or Item.
(Check inline comments for more details)
(Check inline comments for more details)
using (SPSite oSite = new SPSite(webURL)) { using (SPWeb web = oSite.OpenWeb()) { //Get the Service Context of the current Site SPServiceContext context = SPServiceContext.GetContext(oSite); //Get the Social Manager application configured to the current Site Context SocialTagManager socialTagManager = new SocialTagManager(context); //Get the UserProfile Manager application configured to the current Site Context UserProfileManager profileManager = new UserProfileManager(context); TaxonomySession taxSession = socialTagManager.TaxonomySession; //Get the Default keywords TermStore, to which the new Term is going to be created TermStore termStore = taxSession.DefaultKeywordsTermStore; // Check whether the term "I am Following" is already exists in the Term Store TermCollection myTermColl = termStore.KeywordsTermSet.GetTerms("I am Following", true); Term myTerm; // If the Count is greater than 0, requested term is already exists in Term Store if (myTermColl.Count == 0) { //Creates a New Term to the TermStore. myTerm = termStore.KeywordsTermSet.CreateTerm("I am Following", termStore.DefaultLanguage); termStore.CommitAll(); } else { myTerm = myTermColl[0]; } SPList lstDiscussion = web.Lists[txtListName.Text]; SPListItem item = lstDiscussion.GetItemById(Convert.ToInt32(txtID.Text)); Uri itemUri = new Uri(web.Url + "/" + item.Url); if (((LinkButton)e.CommandSource).Text.ToLower() == "follow") { //Add a Tag to the List Item with the above retrieved Term. socialTagManager.AddTag(itemUri, myTerm); ((LinkButton)e.CommandSource).Text = "UnFollow"; } else { //Delete the Tag to the List Item with the above retrieved Term. socialTagManager.DeleteTag(itemUri, myTerm); ((LinkButton)e.CommandSource).Text = "Follow"; } } }
No comments:
Post a Comment