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)));
                }
            }
  }

Code Result





Comments

Popular posts from this blog

Azure - Manage Blob Storage - Part #7 - Add Metadata to an Existing Container using C#

Azure - Manage Blob Storage - Part #5 - Create Folder Structure and upload a file to folder using an Existing Container using C#

Algorithm - Breadth First Search(BFS) - Python(3)