Reading Managed Meta Data Terms Programatically in Share Point

Leave a Comment
Hi All,

Please find the below Code Snippet to Read the Manage Metadata Terms. As an Example iam binding the same to dropdownlist.

private void BindMMS()
{
     ddlMMS.DataSource=MMS_Terms("http://siteUrl:11");
     ddlMMS.DataBind();
}
public List<string> MMS_Terms(string siteUrl)
 {
  List<string> MMS_Term = new List<string>();
  try
  {
   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
    using (SPSite oSite = new SPSite(siteUrl))
     {
      using (SPWeb oWeb = oSite.OpenWeb())
       {
         oWeb.AllowUnsafeUpdates = true;
         TaxonomySession _TaxonomySession = new TaxonomySession(oSite);
         //Get instance of the Term Store
         TermStore _TermStore = 
                         _TaxonomySession.TermStores["ManagedMetaDataServiceAppName"];
         //Get instance of Group
         Group group = _TermStore.Groups.FirstOrDefault(e => e.Name =="Name of the Group");
        
         //Get the termset 
         TermSet termSet_ProjectTerms = group.TermSets.FirstOrDefault(e => e.Name == "Name of                                                                         the Termset");
         
         Term MMS = termSet_ProjectTerms.Terms.FirstOrDefault(t => t.Name =="Name of Term");
        
         MMS_Term = (from b in MMS.Terms
                    select b.Name).ToList();
      }
     }
  });
 }
catch (Exception ex)
{
// exception
}
return MMS_Term;
}

Related Post

0 comments:

Post a Comment