Wednesday, 18 January 2017

Export Managed Metadata Terms to CSV File Using Client Object Model

In SharePoint, Managed Metadata service provides the "Import Term Set" Out of the Box.
Unfortunately it does not provide the same "Export Term Set" capability.

So, I have this CSOM code to export the SharePoint Managed Metadata term store into a CSV file.

The Sample CSV File  has the columns "Term Set Name","Term Set Description","LCID","Available for Tagging","Term Description","Level 1 Term","Level 2 Term".


public void GetTermsFromCloud(ClientContext clientContext)
{
TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
clientContext.Load(termStore,
store => store.Name,
store => store.Groups.Include(
group => group.Name,
group => group.TermSets.IncludeWithDefaultProperties(
termSet => termSet.Name,
termset => termset.Description,
termset => termset.IsAvailableForTagging,
termSet => termSet.Terms.IncludeWithDefaultProperties(
term => term.Name,
term => term.Description,
term => term.Terms.IncludeWithDefaultProperties(
term1 => term1.Name,
term1 => term1.Description
)
)
)
)
);
clientContext.ExecuteQuery();
var csv = new StringBuilder();
var column1 = "Term Set Name";
var column2 = "Term Set Description";
var column3 = "LCID";
var column4 = "Available for Tagging";
var column5 = "Term Description";
var column6 = "Level 1 Term";
var column7 = "Level 2 Term";
var newLine = string.Format("{0},{1},{2},{3},{4},{5},{6}", column1, column2, column3, column4, column5, column6, column7);
csv.AppendLine(newLine);
if (taxonomySession != null)
{
if (termStore != null)
{
foreach (TermGroup group in termStore.Groups)
{
if (group.Name == "GroupName")
{
Console.WriteLine("Group " + group.Name);
foreach (TermSet termSet in group.TermSets)
{
column1 = termSet.Name;
column2 = termSet.Description;
column3 = "1033";
column4 = termSet.IsAvailableForTagging ? "TRUE" : "FALSE";
column5 = "";
column6 = "";
column7 = "";
newLine = string.Format("{0},{1},{2},{3},{4},{5},{6}", column1, column2, column3, column4, column5, column6, column7);
csv.AppendLine(newLine);
Console.WriteLine("TermSet " + termSet.Name);
foreach (Term term in termSet.Terms)
{
column1 = "";
column2 = "";
column3 = "";
column4 = "";
column5 = term.Description;
column6 = term.Name;
column7 = "";
newLine = string.Format("{0},{1},{2},{3},{4},{5},{6}", column1, column2, column3, column4, column5, column6, column7);
csv.AppendLine(newLine);
Console.WriteLine("Term " + term.Name);
foreach (Term term1 in term.Terms)
{
column1 = "";
column2 = "";
column3 = "";
column4 = "";
column5 = term1.Description;
column7 = term1.Name;
column6 = term.Name;
newLine = string.Format("{0},{1},{2},{3},{4},{5},{6}", column1, column2, column3, column4, column5, column6, column7);
csv.AppendLine(newLine);
Console.WriteLine("Term level2 " + term1.Name);
}
}
Console.ReadLine();
}
}
}
}
}
file.File.WriteAllText("filepath\fileName.csv", csv.ToString());
Console.WriteLine("File Saved to Location filePath\fileName.csv");
Console.ReadLine();
}


Thanks!!

Wednesday, 12 October 2016

How to Like list item in SharePoint 2013 Programmatically using CSOM

In this blog you will learn how to implement Like in SharePoint List Programmatically.
Here in this code we have explored about Likes fields and how users like any Custom List item.
Operations I am performing here
  • Get the List
  • Get the List Items
  • Get Site Users
  • Get Liked Users And Likes Count


Web web = clientContext.Web
List list = web.Lists.GetByTitle("Products");
CamlQuery queryItem = new CamlQuery();
queryItem.ViewXml = "<View><Query><Where><Eq><FieldRef Name=\"Title\"/><Value Type=\"Text\">Spices</Value></Eq></Where></Query></View>";
ListItemCollection listItems = list.GetItems(queryItem);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
ListItem item = listItems[0];
clientContext.Load(web.SiteUsers);
clientContext.ExecuteQuery();
User user = web.SiteUsers[0];
foreach (User newUser in clientContext.Web.SiteUsers)
if (newUser.Title == "Arzoo Soni")
{
user = newUser;
break;
}
bool hasLiked = false;
FieldUserValue[] likedByUsers = (FieldUserValue[])item["LikedBy"];
List<FieldUserValue> newFieldUsers = new List<FieldUserValue>();
if (likedByUsers != null && likedByUsers.Length > 0)
foreach (FieldUserValue likedFieldUser in likedByUsers)
if (likedFieldUser.LookupValue == user.Title)
hasLiked = true;
if (!hasLiked)
{
if (likedByUsers != null)
foreach (FieldUserValue likedFieldUser in likedByUsers)
newFieldUsers.Add(likedFieldUser);
newFieldUsers.Add(FieldUserValue.FromUser(user.LoginName));
item["LikedBy"] = newFieldUsers;
item["LikesCount"] = Convert.ToInt32(item["LikesCount"]) + 1;
item.Update();
clientContext.Load(item);
clientContext.ExecuteQuery();
}
view raw LikeItem.cs hosted with ❤ by GitHub

Thanks for reading!!