Following code shows how to get the term collection by passing the list and managed metadata field name.


        public TermCollection getSiteCategoryTerms(SPWeb oWeb, SPList oList, string mmsColumnName)
        {
            try
            {
                TaxonomySession session = new TaxonomySession(oWeb.Site);
                TaxonomyField oField = oList.Fields[mmsColumnName] as TaxonomyField;
                TermSet oSet = session.DefaultSiteCollectionTermStore.GetTermSet(oField.TermSetId);
                TermCollection oColl = oSet.Terms;
                return oColl;
            }
            catch (Exception exp)
            {
                // Log Exception
                return null;
            }
        }

Now we can iterate through each term in term collection using foreach:

foreach (Term oTerm in termColl)
                {
                    if (oTerm.Name == "SomeName")
                    {
                    }
                }
Advertisements