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).

Sunday, July 6, 2014

SharePoint 2013 - The .Net Client-Side Object Model (CSOM)

In this post, we will see an overview of .net client-side object model (CSOM). And in the next post we will see demo to retrieve all items from a SharePoint list using .Net Client-Side Object Model (CSOM).

Classes in Microsoft.SharePoint.Client
As compared to server object model, the client side object model has a lot of identical classes with a suffix in the namespace and no SP prefix in the class name.
Client Object ModelServer Object Model
ClientContextSPContext
SiteSPSite
WebSPWeb
ListSPList


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


The ClientContext instance
An instance of the ClientContext class in Microsoft.SharePoint.Client namespace should be created to refer to the target Site Collection. ClientContext class is equivalent to SPContext class in object model and stands as proxy to the SharePoint server that you are targeting
ClientContext ctx = new ClientContext("http://SiteCollectionUrl/");

Authenticating the ClientContext instance
By default, the CSOM uses Windows integrated authentication. The ClientContext class, through its ClientRuntimeContext base class, provides an AuthenticationMode property and a FormsAuthenticationLoginInfo property, which are useful to configure a set of forms-based authentication credentials.
NetworkCredential credentials = new NetworkCredential("username""password""domain")
ctx.Credentials = credentials;

Loading the objects using Load<T> method
Whenever we need to access an object, we have to first request to load that object using Load<T> method of the ClientContext instance. The following example shows how to load the site, web and list objects.
Site site = ctx.Site;
ctx.Load(site);
Web web = site.RootWeb; 
ctx.Load(web);
List list = web.Lists.GetByTitle("Products");
ctx.Load(list);

The ExecuteQuery Method
Once all the objects are loaded, the ExecuteQuery method should be invoked to complete the action on the site, web or list. There is also an asynchronous version of this method, called ExecuteQueryAsync, for invoking the service asynchronously.
ctx.ExecuteQuery();

Adding CAML Query
To improve performance and reduce network traffic, the data retrieval engine of the Client Object Model by default does not retrieve all of the properties of the items you load. You can use the CAML query definition to specify the fields to retrieve, setting the ViewFields property.
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection allItems = list.GetItems(query);
ctx.Load(allItems);
ctx.ExecuteQuery();

Hope this post gives you an idea about the .Net Client-Side Object Model (CSOM). In the next post we will see demo to retrieve all items from a SharePoint list using .Net Client-Side Object Model (CSOM).