CAML QUERY to retrieve the items from a list using CSOM

Here you can pass an undefined CamlQuery object to return all items from the list, or use the ViewXml property to define a CAML query and return items that meet specific criteria.The following example displays the ID, as well as the Title and Body column values, of the first 100 items in the NewBook list, starting with list items whose collection ID is greater than 19.
 
Steps
  1. Open Visual Studio in your system
  2. Select Console Applciation template and give as name "RetrieveListItems"
  3. Add a Microsoft.Cleint Assembly refrence file in right side refrence tab in visual studio.
  4. Replace Program.cs with the source code below.
  1. using System;  
  2. using Microsoft.SharePoint.Client;  
  3. using SP = Microsoft.SharePoint.Client;  
  4.   
  5. namespace Microsoft.SDK.SharePointServices.Samples  
  6. {  
  7.     class RetrieveListItems  
  8.     {  
  9.         static void Main()  
  10.         {  
  11.             string siteUrl = "http://gauti.sharepoint.com/sites/SP";  
  12.   
  13.             ClientContext clientContext = new ClientContext(siteUrl);  
  14.             SP.List oList = clientContext.Web.Lists.GetByTitle("NewBook");  
  15.   
  16.             CamlQuery camlQuery = new CamlQuery();  
  17.             camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +  
  18.                 "<Value Type='Number'>19</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";  
  19.   
  20.   
  21.             ListItemCollection collListItem = oList.GetItems(camlQuery);  
  22.   
  23.             clientContext.Load(collListItem);  
  24.   
  25.             clientContext.ExecuteQuery();  
  26.   
  27.             foreach (ListItem oListItem in collListItem)  
  28.             {  
  29.                 Console.WriteLine("ID: {0} \nTitle: {1} \nBody: {2}", oListItem.Id, oListItem["Title"], oListItem["Body"]);  
  30.             }  
  31.         }  
  32.     }  


Hit F5 and see the magix happend in SharePoint 2013. Hope you have enjoyed this.!!! :-)
Ebook Download
View all
Learn
View all