Reference Link
http://gallery.technet.microsoft.com/office/Import-Export-Meta-Data-b94ad856/view/Discussions#content
from farm to farm
http://tomislavspadmin.blogspot.in/2014/01/export-term-set-and-import-term-set-in.html
http://sharepointtrac.wordpress.com/2013/10/24/powershell-script-to-export-and-import-managed-metadata-termstore-across-sharepoint-farms-while-still-retaining-its-guids/
http://underthehood.ironworks.com/2011/10/sharepoint-2010-how-to-backuprestore-managed-metadata-from-one-farmenvironment-to-another.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint;
using System.Xml;
using System.Reflection;
using Microsoft.SharePoint.Administration;
namespace ImportExportSPTerm
{
public partial class Form1 : Form
{
static StringBuilder strExportXml;
static TaxonomySession session;
static TermStore termStore;
static Group group;
static TermSet termSet;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
{
}
private void butexport_Click(object sender, EventArgs e)
{
string directoryPath = txtpathExport.Text; //get directory path for export
string groupName = Convert.ToString(cbTermGroup.SelectedItem); //TermStore GroupName
string TermSetname = Convert.ToString(cbTermset.SelectedItem); // TermSet Name
string SPSiteURL = Convert.ToString(cbWebApp.SelectedItem); // site url.
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
ExportTerms(directoryPath, groupName, TermSetname, SPSiteURL);
});
string msg = "Export done....Check your " + groupName + "-" + TermSetname + ".xml file on Location Path " + directoryPath;
MessageBox.Show(msg);
}
catch (Exception ex)
{
MessageBox.Show("Error :" + ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
string filedicpath = txtimportpath.Text;
string SPSiteurl = Convert.ToString(cbImportwebapp.SelectedItem); // site url.
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
ImportTerms(filedicpath, SPSiteurl);
});
MessageBox.Show("Import Done.");
}
catch (Exception ex)
{
MessageBox.Show("Error :" + ex.Message);
}
}
#region Import Code
private static void ImportTerms(string filePath, string urlSPSite)
{
using (SPSite site = new SPSite(urlSPSite))
{
// function that return termstore from site.
termStore = termstorefromWebApp(site);
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNode termGroup = doc.SelectSingleNode("/Groups/Group");
ProcessGroup(termGroup);
termStore.CommitAll();
}
}
private static void ProcessGroup(XmlNode termGroup)
{
if (termGroup != null)
{
string strGroupName = termGroup.Attributes["Name"].Value;
//Get TermStore Groups
GroupCollection groups = termStore.Groups;
//Find group that we want to Import to
Group thisGroup = groups.Where(g => g.Name == strGroupName).SingleOrDefault();
//Check that group exist
if (thisGroup != null)
{
//Get Current group
group = termStore.GetGroup(thisGroup.Id);
}
else //If group doesn't exist, create it
{
group = termStore.CreateGroup(strGroupName);
termStore.CommitAll();
}
Console.WriteLine(strGroupName);
ProcessTermSet(termGroup);
}
}
private static void ProcessTermSet(XmlNode termGroup)
{
foreach (XmlNode termSets in termGroup.ChildNodes)
{
//ProcessTermSet
string strTermSetName = termSets.Attributes["Name"].Value;
string strTermSetNameFr = termSets.Attributes["NameFr"].Value;
//Get TermSetCollection
TermSetCollection termSetColl = group.TermSets;
//Find termSet that we want to Import to
TermSet thisTermSet = termSetColl.Where(g => g.Name == strTermSetName).SingleOrDefault();
if (thisTermSet != null)
{
//Get Current TermSet
termSet = thisTermSet;
//Delete the current TermSet and all the associate terms
termSet.Delete();
termStore.CommitAll();
}
//Create the TermSet
termSet = group.CreateTermSet(strTermSetName, 1033);
termStore.CommitAll();
Console.WriteLine(strTermSetName);
//ProcessTerms
ProcessTerms(termSets.FirstChild, termSet, null);
}
}
private static void ProcessTerms(XmlNode termSetTerms, TermSet termSet, Term currentTerm)
{
Term newTerm;
foreach (XmlNode termsNames in termSetTerms.SelectNodes("Term"))
{
string strTermName = termsNames.Attributes["Name"].Value;
if (currentTerm != null)
{
newTerm = currentTerm.CreateTerm(strTermName, termStore.DefaultLanguage);
}
else
{
newTerm = termSet.CreateTerm(strTermName, termStore.DefaultLanguage);
// termStore.CommitAll();
}
termStore.CommitAll();
Console.WriteLine(strTermName);
//ProcessLabels
foreach (XmlNode labelNames in termsNames.SelectSingleNode("Labels").ChildNodes)
{
string strLCID = labelNames.Attributes["lcid"].Value;
string strLabelName = labelNames.Attributes["Name"].Value;
string strTermDescription = labelNames.Attributes["Description"].Value;
if (Convert.ToInt32(strLCID) != termStore.DefaultLanguage)
{
newTerm.CreateLabel(strLabelName, Convert.ToInt32(strLCID), true);
termStore.CommitAll();
}
newTerm.SetDescription(strTermDescription, Convert.ToInt32(strLCID));
termStore.CommitAll();
Console.WriteLine(strLCID);
Console.WriteLine(strLabelName);
}
foreach (XmlNode subTerms in termsNames.SelectNodes("Terms"))
{
ProcessTerms(subTerms, termSet, newTerm);
}
}
}
#endregion
#region Export Code
private static void ExportTerms(string directoryPath, string groupName, string termsetName, string urlSPSite)
{
strExportXml = new StringBuilder();
using (SPSite site = new SPSite(urlSPSite))
{
String fileName = groupName + "-" + termsetName + ".xml";
fileName = fileName.Replace("/", "").Replace("\\", "").Replace(" ", "");
// function that return termstore from site.
termStore = termstorefromWebApp(site);
Group group = termStore.Groups[groupName];
TermSet termSet = group.TermSets[termsetName];
String termsetNameFr = String.Empty;
try
{
PropertyInfo namesProperty = termSet.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "Names");
System.Collections.Generic.Dictionary<int, string> names = (System.Collections.Generic.Dictionary<int, string>)namesProperty.GetValue(termSet, null);
if (names[1036] != null)
{
termsetNameFr = names[1036];
}
}
catch (System.Collections.Generic.KeyNotFoundException)
{
}
strExportXml.Append("<?xml version=\"1.0\" encoding=\"utf-16\" ?>");
strExportXml.Append("<Groups>");
strExportXml.Append("<Group Name=\"" + groupName + "\">");
strExportXml.Append("<TermSet Name=\"" + termsetName + "\" " + "NameFr=\"" + termsetNameFr + "\">");
strExportXml.Append("<Terms>");
foreach (Term trm in termSet.Terms)
{
GetTerm(trm);
}
strExportXml.Append("</Terms>");
strExportXml.Append("</TermSet>");
strExportXml.Append("</Group>");
strExportXml.Append("</Groups>");
System.Console.WriteLine("Export Terms");
FinalExport(directoryPath, fileName);
}
}
private static void FinalExport(string exportDirectory, string fileName)
{
String path = exportDirectory + fileName;
XmlWriterSettings xmlWritesettings = new XmlWriterSettings();
xmlWritesettings.Indent = true;
xmlWritesettings.OmitXmlDeclaration = true;
xmlWritesettings.Encoding = Encoding.Unicode;
using (XmlWriter writer = XmlWriter.Create(path, xmlWritesettings))
{
writer.WriteRaw(strExportXml.ToString());
}
System.Console.WriteLine("Final Export");
}
private static void GetTerm(Term term)
{
strExportXml.Append("<Term Name=\"" + term.Name.ToString() + "\">");
strExportXml.Append("<Labels>");
foreach (Microsoft.SharePoint.Taxonomy.Label lbl in term.Labels)
{
strExportXml.Append("<Label lcid=\"" + lbl.Language.ToString() + "\" Name=\"" + lbl.Value + "\" Description=\"" + term.GetDescription(lbl.Language) + "\" />");
// Console.WriteLine(" Label: " + lbl.Value + " LCID: " + lbl.Language.ToString());
}
strExportXml.Append("</Labels>");
foreach (Term subtrm in term.Terms)
{
strExportXml.Append("<Terms>");
GetTerm(subtrm);
strExportXml.Append("</Terms>");
}
strExportXml.Append("</Term>");
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
// #### bind combox of all web app url.
SPFarm Farm = SPFarm.Local;
SPWebService service = Farm.Services.GetValue<SPWebService>("");
foreach (SPWebApplication webapp in service.WebApplications)
{
cbWebApp.Items.Add(webapp.GetResponseUri(SPUrlZone.Default).AbsoluteUri);
cbImportwebapp.Items.Add(webapp.GetResponseUri(SPUrlZone.Default).AbsoluteUri);
}
// #### bind combox of all web app url.
}
private void cbWebApp_SelectedIndexChanged(object sender, EventArgs e)
{
string urlSPSite = Convert.ToString(cbWebApp.SelectedItem);
using (SPSite osite = new SPSite(urlSPSite))
{
termStore = termstorefromWebApp(osite);
cbTermGroup.Text = "";
cbTermGroup.Items.Clear();
foreach (Group group in termStore.Groups)
{
cbTermGroup.Items.Add(group.Name);
}
}
}
private void cbTermGroup_SelectedIndexChanged(object sender, EventArgs e)
{
string urlSPSite = Convert.ToString(cbWebApp.SelectedItem);
using (SPSite osite = new SPSite(urlSPSite))
{
termStore = termstorefromWebApp(osite);
string selectedgroupname = Convert.ToString(cbTermGroup.SelectedItem);
Group group = termStore.Groups[selectedgroupname];
cbTermset.Text = "";
cbTermset.Items.Clear();
foreach (TermSet termset in group.TermSets)
{
cbTermset.Items.Add(termset.Name);
}
}
}
public static TermStore termstorefromWebApp(SPSite site)
{
TaxonomySession session = new TaxonomySession(site);
//ThroubleShooting problem during get termstore
// http://sharepoint.stackexchange.com/questions/4097/why-are-taxonomy-term-stores-always-empty
//http://blog.henryong.com/2012/03/20/sharepoint-dev-getting-null-taxonomysession-and-argumentoutofrangeexception-errors-on-termstore-objects/
// 1st way;
termStore = session.TermStores["Managed Metadata Service"];
// 2nd way;
// termStore = session.DefaultKeywordsTermStore;//Try session.DefaultSiteCollectionTermStore
return termStore;
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.AddExtension = true;
openFileDialog.Multiselect = false;
openFileDialog.Filter = "(*.xml)|*.xml";
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (string fileName in openFileDialog.FileNames)
{
txtimportpath.Text = fileName;
}
}
}
}
}
designer
namespace ImportExportSPTerm
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblexport = new System.Windows.Forms.Label();
this.butexport = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.txtpathExport = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.txtimportpath = new System.Windows.Forms.TextBox();
this.cbWebApp = new System.Windows.Forms.ComboBox();
this.cbImportwebapp = new System.Windows.Forms.ComboBox();
this.cbTermGroup = new System.Windows.Forms.ComboBox();
this.cbTermset = new System.Windows.Forms.ComboBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblexport
//
this.lblexport.AutoSize = true;
this.lblexport.Location = new System.Drawing.Point(29, 9);
this.lblexport.Name = "lblexport";
this.lblexport.Size = new System.Drawing.Size(78, 13);
this.lblexport.TabIndex = 0;
this.lblexport.Text = "Export Termset";
//
// butexport
//
this.butexport.Location = new System.Drawing.Point(33, 130);
this.butexport.Name = "butexport";
this.butexport.Size = new System.Drawing.Size(75, 23);
this.butexport.TabIndex = 1;
this.butexport.Text = "Export";
this.butexport.UseVisualStyleBackColor = true;
this.butexport.Click += new System.EventHandler(this.butexport_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(29, 36);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(76, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Location Path:";
//
// txtpathExport
//
this.txtpathExport.Location = new System.Drawing.Point(152, 29);
this.txtpathExport.Name = "txtpathExport";
this.txtpathExport.Size = new System.Drawing.Size(311, 20);
this.txtpathExport.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(29, 55);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(113, 13);
this.label2.TabIndex = 4;
this.label2.Text = "Web Application URL:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(30, 78);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 13);
this.label3.TabIndex = 6;
this.label3.Text = "GroupName";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(30, 104);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(75, 13);
this.label4.TabIndex = 9;
this.label4.Text = "TermSetName";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(30, 163);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(77, 13);
this.label5.TabIndex = 10;
this.label5.Text = "Import Termset";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(30, 216);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(94, 13);
this.label6.TabIndex = 11;
this.label6.Text = "Location Path(xml)";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(30, 187);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(113, 13);
this.label7.TabIndex = 13;
this.label7.Text = "Web Application URL:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(33, 253);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 15;
this.button1.Text = "Import";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txtimportpath
//
this.txtimportpath.Location = new System.Drawing.Point(152, 216);
this.txtimportpath.Name = "txtimportpath";
this.txtimportpath.Size = new System.Drawing.Size(311, 20);
this.txtimportpath.TabIndex = 16;
//
// cbWebApp
//
this.cbWebApp.FormattingEnabled = true;
this.cbWebApp.Location = new System.Drawing.Point(152, 55);
this.cbWebApp.Name = "cbWebApp";
this.cbWebApp.Size = new System.Drawing.Size(311, 21);
this.cbWebApp.TabIndex = 17;
this.cbWebApp.Text = "--Select webapp url--";
this.cbWebApp.SelectedIndexChanged += new System.EventHandler(this.cbWebApp_SelectedIndexChanged);
//
// cbImportwebapp
//
this.cbImportwebapp.FormattingEnabled = true;
this.cbImportwebapp.Location = new System.Drawing.Point(152, 189);
this.cbImportwebapp.Name = "cbImportwebapp";
this.cbImportwebapp.Size = new System.Drawing.Size(311, 21);
this.cbImportwebapp.TabIndex = 18;
this.cbImportwebapp.Text = "--Select webapp url--";
//
// cbTermGroup
//
this.cbTermGroup.FormattingEnabled = true;
this.cbTermGroup.Location = new System.Drawing.Point(152, 82);
this.cbTermGroup.Name = "cbTermGroup";
this.cbTermGroup.Size = new System.Drawing.Size(311, 21);
this.cbTermGroup.TabIndex = 19;
this.cbTermGroup.SelectedIndexChanged += new System.EventHandler(this.cbTermGroup_SelectedIndexChanged);
//
// cbTermset
//
this.cbTermset.FormattingEnabled = true;
this.cbTermset.Location = new System.Drawing.Point(152, 109);
this.cbTermset.Name = "cbTermset";
this.cbTermset.Size = new System.Drawing.Size(311, 21);
this.cbTermset.TabIndex = 20;
//
// button2
//
this.button2.Location = new System.Drawing.Point(469, 216);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 20);
this.button2.TabIndex = 21;
this.button2.Text = "Upload";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(681, 326);
this.Controls.Add(this.button2);
this.Controls.Add(this.cbTermset);
this.Controls.Add(this.cbTermGroup);
this.Controls.Add(this.cbImportwebapp);
this.Controls.Add(this.cbWebApp);
this.Controls.Add(this.txtimportpath);
this.Controls.Add(this.button1);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtpathExport);
this.Controls.Add(this.label1);
this.Controls.Add(this.butexport);
this.Controls.Add(this.lblexport);
this.Name = "Form1";
this.Text = "Import Export SPTerm";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblexport;
private System.Windows.Forms.Button butexport;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtpathExport;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtimportpath;
private System.Windows.Forms.ComboBox cbWebApp;
private System.Windows.Forms.ComboBox cbImportwebapp;
private System.Windows.Forms.ComboBox cbTermGroup;
private System.Windows.Forms.ComboBox cbTermset;
private System.Windows.Forms.Button button2;
}
}
xml file
<?xml version="1.0" encoding="utf-16" ?>
<Groups>
<Group Name="Intranet">
<TermSet Name="Country" NameFr="">
<Terms>
<Term Name="USA">
<Labels>
<Label lcid="1033" Name="USA" Description="" />
</Labels>
</Term>
<Term Name="CANADA">
<Labels>
<Label lcid="1033" Name="CANADA" Description="" />
</Labels>
</Term>
<Term Name="INDIA">
<Labels>
<Label lcid="1033" Name="INDIA" Description="" />
</Labels>
</Term>
</Terms>
</TermSet>
</Group>
</Groups>
No comments:
Post a Comment