Saturday, July 26, 2014

SharePoint 2013 - Retrieve all items from a SharePoint list using .Net Client-Side Object Model (CSOM)

In the previous post we discussed the basics of .Net Client-Side Object Model (CSOM). In this post, we will see how to implement CSOM to access a SharePoint list from a client side console application. Following are the steps:

Create a custom list for retrieving data from client side
Create a custom list named Products as shown in the figure below. We will create a windows console application to retrieve data from this list using CSOM.

Locate the assemblies required for implementing .Net CSOM
Set of managed assemblies for CSOM reside in SharePoint15_Root\ISAPI. The following 2 assemblies are the minimum required assemblies for CSOM.
(i) Microsoft.SharePoint.Client.dll
(ii) Microsoft.SharePoint.Client.Runtime.dll

Add the CSOM assembly reference
Create a windows console application in Visual studio and add  the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll as reference.

Modify the Main Method with following code
//Create the ClientContext instance
ClientContext ctx = new ClientContext("http://SiteCollectionUrl/");

//Authenticate the ClientContext instance
NetworkCredential credentials = new NetworkCredential("username""password""domain")
ctx.Credentials = credentials;

//Load the objects using Load<T> method
Site site = ctx.Site;
ctx.Load(site);
Web web = site.RootWeb;
ctx.Load(web);
List list = web.Lists.GetByTitle("Products");
ctx.Load(list);

//Add CAML Query
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection allItems = list.GetItems(query);
ctx.Load(allItems);

//Execute the Query
ctx.ExecuteQuery();

Following is the complete code for our console application using CSOM.
Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.SharePoint.Client;
  7. using System.Net;
  8.  
  9. namespace CSOMDemo
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             // Enter your network credentials
  16.             NetworkCredential credentials = new NetworkCredential("username""password""domain");
  17.             
  18.             // Load the site collection using ClientContext
  19.             ClientContext ctx = new ClientContext("http://SiteCollectionUrl/");            
  20.             ctx.Credentials = credentials;
  21.             Site site = ctx.Site;
  22.             ctx.Load(site);
  23.  
  24.             // Load the "Products" list
  25.             Web web = site.RootWeb; ctx.Load(web);
  26.             List list = web.Lists.GetByTitle("Products");
  27.             ctx.Load(list);
  28.             
  29.             // List load is completed only when ExecuteQuery() or ExecuteQueryAsnc() is called
  30.             ctx.ExecuteQuery();
  31.  
  32.             // Display the name of list
  33.             Console.WriteLine(list.Title+"\n");
  34.             
  35.             // Following query returns all items in the list
  36.             CamlQuery query = new CamlQuery();
  37.             query.ViewXml = "<View/>";
  38.             ListItemCollection allItems = list.GetItems(query);
  39.             ctx.Load(allItems);
  40.             ctx.ExecuteQuery();
  41.  
  42.             // Display List Item column names (Title, Description and Price)
  43.             Console.WriteLine("Title" + "\t\t" + "Description" + "\t\t" + "Price");
  44.  
  45.             // Loop through all items and display the list items (Title, Description and Price)
  46.             foreach (ListItem listItem in allItems)
  47.             {
  48.                 Console.WriteLine(listItem["Title"].ToString() + "\t" + listItem["Description"].ToString() + "\t" + listItem["Price"].ToString());
  49.             }
  50.  
  51.             Console.Read();
  52.         }
  53.     }
  54. }

Displaying the data
Run the console application. Data will be retrieved from the Products list and displayed in the console.

Hope this post gives you an idea about the .Net Client-Side Object Model (CSOM).

No comments:

Post a Comment