This blog will continue The steps of migrating metadata from SP site 2010 to SP site 2013, and the below is about The base operation about Metadata Service
public class ClientMetadataService
{
public List<string> getMetadaService(string siteUrl)
{
List<string> metadataServices = new List<string>();
using (SPSite site = new SPSite(siteUrl))
{
TaxonomySession session = new TaxonomySession(site);
TermStoreCollection termStores = session.TermStores;
foreach (TermStore termStore in termStores)
{
metadataServices.Add(termStore.Name.ToString());
}
}
return metadataServices;
}
public List<string> getGroups(SPSite site, string metadataServiceName)
{
List<string> metadataGroups = new List<string>();
try
{
TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.TermStores[metadataServiceName];
Microsoft.SharePoint.Taxonomy.GroupCollection groups = termStore.Groups;
foreach (Microsoft.SharePoint.Taxonomy.Group group in groups)
{
metadataGroups.Add(group.Name.ToString());
}
}
catch { }
return metadataGroups;
}
public List<string> getTermSets(SPSite site, string metadataServiceName, string groupName)
{
List<string> metadataTermSets = new List<string>();
try
{
TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.TermStores[metadataServiceName];
Microsoft.SharePoint.Taxonomy.GroupCollection groups = termStore.Groups;
Microsoft.SharePoint.Taxonomy.Group group = termStore.Groups[groupName];
Microsoft.SharePoint.Taxonomy.TermSetCollection termSets = group.TermSets;
foreach (TermSet termSet in termSets)
{
metadataTermSets.Add(termSet.Name.ToString());
}
}
catch { }
return metadataTermSets;
}
public Term circleTerm(Term term, string termName)
{
Term newTerm;
Term newTerm2;
int termsCount = term.Terms.Count;
if (term.Labels[0].Value == termName)
{
return term;
}
else if (termsCount > 0)
{
for (int i = 0; i < termsCount; i++)
{
newTerm = term.Terms[i];
newTerm2 = circleTerm(newTerm, termName);
if (newTerm2 != null)
{
return newTerm2;
}
else
{
continue;
}
}
}
return null;
}
}