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.
(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.
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).
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using System.Net;
- namespace CSOMDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Enter your network credentials
- NetworkCredential credentials = new NetworkCredential("username", "password", "domain");
- // Load the site collection using ClientContext
- ClientContext ctx = new ClientContext("http://SiteCollectionUrl/");
- ctx.Credentials = credentials;
- Site site = ctx.Site;
- ctx.Load(site);
- // Load the "Products" list
- Web web = site.RootWeb; ctx.Load(web);
- List list = web.Lists.GetByTitle("Products");
- ctx.Load(list);
- // List load is completed only when ExecuteQuery() or ExecuteQueryAsnc() is called
- ctx.ExecuteQuery();
- // Display the name of list
- Console.WriteLine(list.Title+"\n");
- // Following query returns all items in the list
- CamlQuery query = new CamlQuery();
- query.ViewXml = "<View/>";
- ListItemCollection allItems = list.GetItems(query);
- ctx.Load(allItems);
- ctx.ExecuteQuery();
- // Display List Item column names (Title, Description and Price)
- Console.WriteLine("Title" + "\t\t" + "Description" + "\t\t" + "Price");
- // Loop through all items and display the list items (Title, Description and Price)
- foreach (ListItem listItem in allItems)
- {
- Console.WriteLine(listItem["Title"].ToString() + "\t" + listItem["Description"].ToString() + "\t" + listItem["Price"].ToString());
- }
- Console.Read();
- }
- }
- }
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
Note: Only a member of this blog may post a comment.