SharePoint - Client Side Object Model(CSOM) - Managed - Get List Fields
Problem Definition
Retrieve List fields with details using Managed Code and Client Side Object Model(CSOM) for SharePoint 2013.
Prerequisites
Code Example
/// <summary>
/// Print list field names
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetFields(string listTitle)
{
//var spContext = new ClientContext("http://<your server>/<site>/");
using (spContext)
{
// Client side SP Web Conext
var webContext = spContext.Web;
// Initilize web and load web object
spContext.Load(webContext);
// Get list details by list title
var list = webContext.Lists.GetByTitle(listTitle);
// Initilize and load lists fields
spContext.Load(list.Fields);
// Execute the query, which in this case will retrieve the fields
spContext.ExecuteQuery();
// Print the Count of Fields
Console.WriteLine($"Columns Found: {list.Fields.Count}");
// Print the Details with Header
Console.WriteLine("{0} {1} {2} {3}"
, "Title".PadRight(30)
, "InternalName".PadRight(26)
, "Hidden".PadRight(7)
, "Del?");
Console.WriteLine("--------------------------------------------------------------------------------");
// Sort results by hidden field value
var sortedList = list.Fields.ToList().OrderBy(x => x.Hidden);
foreach (Field f in sortedList)
{
Console.WriteLine("{0} | {1} | {2} | {3}"
, f.Title.PadRight(30)
, f.InternalName.PadRight(26)
, (f.Hidden ? "Hidden" : "Visible").PadRight(7)
, f.CanBeDeleted.ToString().PadRight(3));
}
}
}
------------------------------------------Alternative Syntax(Same Result)----------------------------------------
/// <summary>
/// Print list field names
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetFields_Syntax2(string listTitle)
{
//var spContext = new ClientContext("http://<your server>/<site>/");
using (spContext)
{
// Client side SP Web Conext
var web = spContext.Web;
// Initilize web and load web object
spContext.Load(web);
//Get List by Title
var list = web.Lists.GetByTitle(listTitle);
//Cherry Pick fields to include
spContext.Load(list,
l => l.Title
,l=>l.Fields.Include(f => f.Title
,f=>f.InternalName
, f => f.Hidden
, f => f.CanBeDeleted));
//Execute
spContext.ExecuteQuery();
// Print the Count of Fields
Console.WriteLine($"Columns Found: {list.Fields.Count}");
// Print the Details with Header
Console.WriteLine("{0} {1} {2} {3}"
, "Title".PadRight(30)
, "InternalName".PadRight(26)
, "Hidden".PadRight(7)
, "Del?");
Console.WriteLine("--------------------------------------------------------------------------------");
// Sort results by hidden field value
var sortedList = list.Fields.ToList().OrderBy(x => x.Hidden);
foreach (Field f in sortedList)
{
Console.WriteLine("{0} | {1} | {2} | {3}"
, f.Title.PadRight(30)
, f.InternalName.PadRight(26)
, (f.Hidden ? "Hidden" : "Visible").PadRight(7)
, f.CanBeDeleted.ToString().PadRight(3));
}
}
}
----------------------------------------LINQ Query Syntax(Same Result)----------------------------------------
/// <summary>
/// Print list field names
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetFields_LinqSyntax(string listTitle)
{
//var spContext = new ClientContext("http://<your server>/<site>/");
using (spContext)
{
// Client side SP Web Conext
var web = spContext.Web;
// Initilize web and load web object
spContext.Load(web);
//Get List by Title
var list = web.Lists.GetByTitle(listTitle);
//Cherry Pick fields to include using a linq Query
var query = from fl in list.Fields.Include(f => f.Title
, f => f.InternalName
, f => f.Hidden
, f => f.CanBeDeleted)
select fl;
//Load the linq query
var fields = spContext.LoadQuery(query);
//Execute query
spContext.ExecuteQuery();
// Print the Count of Fields
Console.WriteLine($"Columns Found: {fields.Count()}");
// Print the Details with Header
Console.WriteLine("{0} {1} {2} {3}"
, "Title".PadRight(30)
, "InternalName".PadRight(26)
, "Hidden".PadRight(7)
, "Del?");
Console.WriteLine("--------------------------------------------------------------------------------");
// Sort results by hidden field value
var sortedList = fields.ToList().OrderBy(x => x.Hidden);
foreach (Field f in sortedList)
{
Console.WriteLine("{0} | {1} | {2} | {3}"
, f.Title.PadRight(30)
, f.InternalName.PadRight(26)
, (f.Hidden ? "Hidden" : "Visible").PadRight(7)
, f.CanBeDeleted.ToString().PadRight(3));
}
}
}
/// Print list field names
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetFields(string listTitle)
{
//var spContext = new ClientContext("http://<your server>/<site>/");
using (spContext)
{
// Client side SP Web Conext
var webContext = spContext.Web;
// Initilize web and load web object
spContext.Load(webContext);
// Get list details by list title
var list = webContext.Lists.GetByTitle(listTitle);
// Initilize and load lists fields
spContext.Load(list.Fields);
// Execute the query, which in this case will retrieve the fields
spContext.ExecuteQuery();
// Print the Count of Fields
Console.WriteLine($"Columns Found: {list.Fields.Count}");
// Print the Details with Header
Console.WriteLine("{0} {1} {2} {3}"
, "Title".PadRight(30)
, "InternalName".PadRight(26)
, "Hidden".PadRight(7)
, "Del?");
Console.WriteLine("--------------------------------------------------------------------------------");
// Sort results by hidden field value
var sortedList = list.Fields.ToList().OrderBy(x => x.Hidden);
foreach (Field f in sortedList)
{
Console.WriteLine("{0} | {1} | {2} | {3}"
, f.Title.PadRight(30)
, f.InternalName.PadRight(26)
, (f.Hidden ? "Hidden" : "Visible").PadRight(7)
, f.CanBeDeleted.ToString().PadRight(3));
}
}
}
------------------------------------------Alternative Syntax(Same Result)----------------------------------------
/// <summary>
/// Print list field names
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetFields_Syntax2(string listTitle)
{
//var spContext = new ClientContext("http://<your server>/<site>/");
using (spContext)
{
// Client side SP Web Conext
var web = spContext.Web;
// Initilize web and load web object
spContext.Load(web);
//Get List by Title
var list = web.Lists.GetByTitle(listTitle);
//Cherry Pick fields to include
spContext.Load(list,
l => l.Title
,l=>l.Fields.Include(f => f.Title
,f=>f.InternalName
, f => f.Hidden
, f => f.CanBeDeleted));
//Execute
spContext.ExecuteQuery();
// Print the Count of Fields
Console.WriteLine($"Columns Found: {list.Fields.Count}");
// Print the Details with Header
Console.WriteLine("{0} {1} {2} {3}"
, "Title".PadRight(30)
, "InternalName".PadRight(26)
, "Hidden".PadRight(7)
, "Del?");
Console.WriteLine("--------------------------------------------------------------------------------");
// Sort results by hidden field value
var sortedList = list.Fields.ToList().OrderBy(x => x.Hidden);
foreach (Field f in sortedList)
{
Console.WriteLine("{0} | {1} | {2} | {3}"
, f.Title.PadRight(30)
, f.InternalName.PadRight(26)
, (f.Hidden ? "Hidden" : "Visible").PadRight(7)
, f.CanBeDeleted.ToString().PadRight(3));
}
}
}
----------------------------------------LINQ Query Syntax(Same Result)----------------------------------------
/// <summary>
/// Print list field names
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetFields_LinqSyntax(string listTitle)
{
//var spContext = new ClientContext("http://<your server>/<site>/");
using (spContext)
{
// Client side SP Web Conext
var web = spContext.Web;
// Initilize web and load web object
spContext.Load(web);
//Get List by Title
var list = web.Lists.GetByTitle(listTitle);
//Cherry Pick fields to include using a linq Query
var query = from fl in list.Fields.Include(f => f.Title
, f => f.InternalName
, f => f.Hidden
, f => f.CanBeDeleted)
select fl;
//Load the linq query
var fields = spContext.LoadQuery(query);
//Execute query
spContext.ExecuteQuery();
// Print the Count of Fields
Console.WriteLine($"Columns Found: {fields.Count()}");
// Print the Details with Header
Console.WriteLine("{0} {1} {2} {3}"
, "Title".PadRight(30)
, "InternalName".PadRight(26)
, "Hidden".PadRight(7)
, "Del?");
Console.WriteLine("--------------------------------------------------------------------------------");
// Sort results by hidden field value
var sortedList = fields.ToList().OrderBy(x => x.Hidden);
foreach (Field f in sortedList)
{
Console.WriteLine("{0} | {1} | {2} | {3}"
, f.Title.PadRight(30)
, f.InternalName.PadRight(26)
, (f.Hidden ? "Hidden" : "Visible").PadRight(7)
, f.CanBeDeleted.ToString().PadRight(3));
}
}
}
Comments
Post a Comment