using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace ExportTermStoreToCSV
{
class Program
{
static void Main(string[] args)
{
var sbCSV = new StringBuilder();
#region get Termstore Online
Console.Write("Enter Site URL:-");
string SiteUrl = Console.ReadLine();
ClientContext cc = new ClientContext(SiteUrl);
Console.Write("Enter User Name:-");
string Username = Console.ReadLine();
Console.Write("Enter Password:-");
string passwordOnline = Console.ReadLine();
var securePasswordOnline = new SecureString();
foreach (char c in passwordOnline)
{
securePasswordOnline.AppendChar(c);
}
cc.Credentials = new SharePointOnlineCredentials(Username, securePasswordOnline);
TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(cc);
TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
cc.Load(termStore,
store => store.Name,
store => store.Groups.Include(
group => group.Name,
group => group.TermSets.Include(
termSet => termSet.Name, termSet => termSet.Terms.Include(
term => term.Name, term => term.TermsCount, term => term.LocalCustomProperties, term => term.Terms.Include
(
childTerm => term.Name,
childTerm => term.TermsCount,
childTerm => term.LocalCustomProperties,
childTerm => term.Terms.Include(
childTerm2 => term.Name,
childTerm2 => term.TermsCount,
childTerm2 => term.LocalCustomProperties,
childTerm2 => term.Terms.Include(
childTerm3 => term.Name,
childTerm3 => term.TermsCount,
childTerm3 => term.LocalCustomProperties,
childTerm3 => term.Terms.Include(
childTerm4 => term.Name,
childTerm4 => term.TermsCount,
childTerm4 => term.LocalCustomProperties,
childTerm4 => term.Terms.Include(
childTerm5 => term.Name,
childTerm5 => term.TermsCount,
childTerm5 => term.LocalCustomProperties,
childTerm5 => term.Terms.Include(
childTerm6 => term.Name,
childTerm6 => term.TermsCount,
childTerm6 => term.LocalCustomProperties)))))))
)
)
);
cc.ExecuteQuery();
if (taxonomySession != null)
{
if (termStore != null)
{
sbCSV.AppendLine("TermSetName, Level 1, Level 2, Level 3, Level 4, Level 5, Level 6, Level 7");
foreach (TermGroup group in termStore.Groups)
{
Console.WriteLine("Group " + group.Name);
sbCSV.AppendLine(group.Name + ",,,,,,,");
foreach (TermSet termSet in group.TermSets)
{
Console.WriteLine("TermSet " + termSet.Name);
sbCSV.AppendLine(group.Name + "," + termSet.Name + ",,,,,,");
foreach (Term term in termSet.Terms)
{
// Writes root-level terms only.
Console.WriteLine("Term " + term.Name);
sbCSV.AppendLine(group.Name + "," + termSet.Name + "," + term.Name + ",,,,,");
if (term.TermsCount > 0)
{
string str = group.Name + "," + termSet.Name + "," + term.Name;
BindTermStore(term.Terms, str, sbCSV);
}
}
}
}
}
}
#endregion
System.IO.File.WriteAllText(@"D:\Whatever.csv", sbCSV.ToString());
}
/// <summary>
/// Recursive method to check child terms
/// </summary>
/// <param name="termColl">TermCollection termColl</param>
/// <param name="str">string strTermName</param>
/// <param name="sbs">StringBuilder sbs</param>
private static void BindTermStore(TermCollection termColl, string str, StringBuilder sbs)
{
foreach (Term term in termColl)
{
// Writes root-level terms only.
Console.WriteLine("Term " + term.Name);
sbs.AppendLine(str + "," + term.Name);
if (term.TermsCount > 0)
{
string sss = str + "," + term.Name;
BindTermStore(term.Terms, sss, sbs);
}
}
}
}
}
Welcome friends, this blog site will share my experience, challenges, ideas, achievements with the others across the globe.
Tuesday, 16 February 2016
SharePoint 2013 - Export Termstore to CSV using CSOM
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment