Below is the simple code that migrates metadata or taxonomy from one store(metadata service) to other on same farm.
public class TaxonomyUtility
{
public void MigrateTaxonomy(SPSite sourceSite,SPSite destinationSite,string srctermstoreName, string destermStoreName string termGroup, string termSet)
{
TaxonomySession srcTSession = new TaxonomySession(sourceSite);
TaxonomySession desTSession = new TaxonomySession(destinationSite);
TermStore srcTermStore = srcTSession.TermStores[srctermstoreName];
TermStore desTermStore = desTSession.TermStores[destermStoreName];
Group desGroup;
TermSet desTermSet;
Term desTerm;
foreach (Group g in srcTermStore.Groups)
{
// Carry permisssions
//foreach (SPAce
//{
//}
desGroup = desTermStore.CreateGroup(g.Name);
foreach (TermSet tSet in g.TermSets)
{
desTermSet = desGroup.CreateTermSet(tSet.Name, 1033);
foreach (Term t in tSet.Terms)
{
desTerm = desTermSet.CreateTerm(t.Name,1033);
getLabels(t,desTerm);
getTermsRecursive(t,desTerm);
}
}
}
desTermStore.CommitAll();
}
public void getTermsRecursive(Term srcTerm, Term destTerm)
{
if (srcTerm.Terms.Count > 0)
{
foreach (Term sTerm in srcTerm.Terms)
{
destTerm = destTerm.CreateTerm(sTerm.Name, 1033, sTerm.Id);
getLabels(sTerm,destTerm);
getTermsRecursive(sTerm, destTerm);
}
}
else
{
return;
}
}
public void getLabels(Term srcTerm,Term desTerm)
{
foreach (Label l in srcTerm.Labels)
{
desTerm.CreateLabel(l.Value, 1033, l.IsDefaultForLanguage);
}
}
}
}
Voila ...u did it ;-)
Other ways of migrating metadata term store in sharepoint 2010 and their limitations are discussed here.
Other ways of migrating metadata term store in sharepoint 2010 and their limitations are discussed here.
No comments:
Post a Comment