SharePoint - Client Side Object Model(CSOM) - Managed - Get Items Summary
Problem Definition
Retrieve List Items summary using Managed Code and Client Side Object Model(CSOM) for SharePoint 2013.
Prerequisites
Code Example
/// <summary>
/// Print list Items with Summary such as ID, Title and Attachments
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetItems(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 details by list title
var list = web.Lists.GetByTitle(listTitle);
//Create Caml Query
var camlQuery = new CamlQuery();
//use following line if you want to constraint or create query with Get 100 Items Only
//camlQuery.ViewXml = "<View><RowLimit>100</RowLimit></View>";
//Get All Items as query involves no constraints
ListItemCollection collListItem = list.GetItems(camlQuery);
// Initilize and load list item fields such as ID, Title and Attachemnts
spContext.Load(collListItem,
items => items.Include(item => item.Id,item => item.DisplayName,
item => item.AttachmentFiles));
// Execute the query, which in this case will retrieve the selected items
spContext.ExecuteQuery();
// Print the Count of Items
Console.WriteLine($"Items Found: {collListItem.Count}");
// Print the Summary with Header
Console.WriteLine("{0} {1} {2}"
, "ID".PadRight(5)
, "DisplayName".PadRight(26)
, "AttachmentFiles");
Console.WriteLine("--------------------------------------------------------------------------------");
foreach (var li in collListItem)
{
Console.WriteLine("{0} | {1} | {2} "
, li.Id.ToString().PadRight(5)
, li.DisplayName.PadRight(26)
, string.Join(",", li.AttachmentFiles.ToList().Select(x => x.FileName)));
}
}
}
/// Print list Items with Summary such as ID, Title and Attachments
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetItems(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 details by list title
var list = web.Lists.GetByTitle(listTitle);
//Create Caml Query
var camlQuery = new CamlQuery();
//use following line if you want to constraint or create query with Get 100 Items Only
//camlQuery.ViewXml = "<View><RowLimit>100</RowLimit></View>";
//Get All Items as query involves no constraints
ListItemCollection collListItem = list.GetItems(camlQuery);
// Initilize and load list item fields such as ID, Title and Attachemnts
spContext.Load(collListItem,
items => items.Include(item => item.Id,item => item.DisplayName,
item => item.AttachmentFiles));
// Execute the query, which in this case will retrieve the selected items
spContext.ExecuteQuery();
// Print the Count of Items
Console.WriteLine($"Items Found: {collListItem.Count}");
// Print the Summary with Header
Console.WriteLine("{0} {1} {2}"
, "ID".PadRight(5)
, "DisplayName".PadRight(26)
, "AttachmentFiles");
Console.WriteLine("--------------------------------------------------------------------------------");
foreach (var li in collListItem)
{
Console.WriteLine("{0} | {1} | {2} "
, li.Id.ToString().PadRight(5)
, li.DisplayName.PadRight(26)
, string.Join(",", li.AttachmentFiles.ToList().Select(x => x.FileName)));
}
}
}
Comments
Post a Comment