Get Taxonomy Value from Sharepoint 2013

  1. protected void TaxonomyGetTermName(string url, string strTermName)  
  2.        {  
  3.            string valueOut = "";  
  4.              
  5.            try  
  6.            {  
  7.                using (ClientContext clientContext = new ClientContext(url))  
  8.                {  
  9.   
  10.                    clientContext.Credentials = new NetworkCredential("id""pwd""domain");  
  11.                    //  
  12.                    // Load up the taxonomy item names.  
  13.                    //  
  14.                    TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);  
  15.                    TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();  
  16.   
  17.                    clientContext.Load(termStore,  
  18.                    store => store.Name,  
  19.                    store => store.Groups.Include(  
  20.                        group => group.Name,  
  21.                        group => group.TermSets.Include(  
  22.                            termSet => termSet.Name,  
  23.                            termSet => termSet.Terms  
  24.                        ).Where(termSet => termSet.Name == strTermName)        //.Where(termSet => termSet.Name == "termname")    
  25.                    ).Where(group => group.Name == "groupname")  
  26.                     );  
  27.                    clientContext.ExecuteQuery();  
  28.                    if (taxonomySession != null)  
  29.                    {  
  30.                        if (termStore != null)  
  31.                        {  
  32.                            foreach (TermGroup group in termStore.Groups)  
  33.                            {  
  34.                                Console.WriteLine("Group " + group.Name);  
  35.                                foreach (TermSet termSet in group.TermSets)  
  36.                                {  
  37.                                    Console.WriteLine("TermSet " + termSet.Name);  
  38.                                    foreach (Term NameLable in termSet.Terms)  
  39.                                    {  
  40.                                        NameLable.CustomProperties.TryGetValue("idofID"out valueOut );  
  41.                                        DictionaryName.Add(NameLable.Name.ToLower(), valueOut );  
  42.                                    }  
  43.                                }  
  44.                            }  
  45.                        }  
  46.                    }  
  47.   
  48.                      
  49.   
  50.                       
  51.                }  
  52.   
  53.            }  
  54.            catch (Exception ex)  
  55.            {  
  56.                   
  57.            }  
  58.        }