locked
How to add Tag to folder in shared documents programmatically from another asp.net website using server object model RRS feed

  • Question

  • Hi,

    i am creating team site inside my parent site programmatically from another asp.net website using server object model and adding few bunch of folders to the newley added team site, now i need to tag those folders programmatically, please help.

    Monday, April 9, 2012 9:16 AM

Answers

  • Hello HarishKumarBH,

    In order to add SharePoint 2010 Tags programmatically, you need to write code like this:

          string listItemUrl = null;
          using (SPSite site = new SPSite("http://somesite "))
          {
            using (SPWeb web = site.OpenWeb())
            {
              SPListItem item = web.Lists["ListName"].GetItemById(1);
              listItemUrl = web.Url + "/" + item.Url;
            
            }
          
          }
          SPServiceContext context = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default,
              SPSiteSubscriptionIdentifier.Default);
          //Create SocialTagManager Object
          SocialTagManager stm = new SocialTagManager(context);
          //Get the TaxonomySession
          TaxonomySession ts = stm.TaxonomySession;
          
          TermSetCollection termSetCollection = ts.DefaultKeywordsTermStore.GetTermSets("Keywords", 1033);
          TermSet termSet = termSetCollection["Keywords"];
      
          var result = from t in termSet.Terms where t.Name == "I like it" select t;
          //Make sure that the tag name is already existed in the Store
          Term term = stm.TaxonomySession.DefaultKeywordsTermStore.KeywordsTermSet.Terms["I like it"];  
          if(term != null)
                //add the tag to item
            stm.AddTag(new Uri(listItemUrl), term);   
    }

    Please make sure that this code is run under proper identity. And the Tag Name is already existed in the Term store.

    Thanks,


    Lambda Zhao

    TechNet Community Support


    Wednesday, April 11, 2012 3:33 AM