Retrieving List Items From A Specific View Using CSOM

In this article, you will learn how we can access the SharePoint data using Client Object Model. Here, I have taken an example of getting the data from SharePoint list specific View. Using this, we can create our own custom View with necessary fields and access them with the below code.

You can get the SharePoint Client Object Model URLs from the Microsoft site or can download the package from NuGet package.

SharePoint

Steps

  1. Open Visual Studio and create a new console application.
  2. Add CSOM (Client Side Object Model) Library from NuGet package.
  3. Replace the code in Program.cs file.

Sample List 

Here is the sample list created in SharePoint Online and created custom View called RKView.

SharePoint

Code 

Replace the below code in Program.cs file.

Note

As we are working in Client Object Model, we need to load each and every required item and do execute the query to get the complete list of these items. 

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             using (ClientContext context = new ClientContext("https://RKSiteURL.com"))  
  6.             {  
  7.                 SecureString securePassword = GetSecureString(Utility.pwd);  
  8.                 context.Credentials = new SharePointOnlineCredentials(Utility.userName, securePassword);  
  9.   
  10.                 List list = context.Web.Lists.GetByTitle("RK_TestList");  
  11.                 context.Load(list);  
  12.                 context.ExecuteQuery();  
  13.                 View view = list.Views.GetByTitle("RKView");  
  14.   
  15.                 context.Load(view);  
  16.                 context.ExecuteQuery();  
  17.                 CamlQuery query = new CamlQuery();  
  18.                 query.ViewXml = view.ViewQuery;  
  19.   
  20.                 ListItemCollection items = list.GetItems(query);  
  21.                 context.Load(items);  
  22.                 context.ExecuteQuery();  
  23.                 Console.WriteLine("Total Count: " + items.Count);  
  24.   
  25.                 foreach(ListItem itm in items)  
  26.                 {  
  27.                     Console.WriteLine("Name: " + itm["Name"]);  
  28.                     Console.WriteLine("Address: " + itm["Address"]);  
  29.                     Console.WriteLine("Specialization: " + itm["Specialization"]);  
  30.                     Console.WriteLine("Qualification: " + itm["Qualification"]);  
  31.                     Console.WriteLine();  
  32.                 }  
  33.   
  34.                 Console.ReadKey();  
  35.             }  
  36.         }  
  37.         public static SecureString GetSecureString(string userPassword)  
  38.         {  
  39.             SecureString securePassword = new SecureString();  
  40.   
  41.             foreach (char c in userPassword.ToCharArray())  
  42.             {  
  43.                 securePassword.AppendChar(c);  
  44.             }  
  45.   
  46.             return securePassword;  
  47.         }  
  48.     }  

Execute the project

Here is the output. It shows all the details which are present in RKView.

SharePoint

Hope this article will help you. Thank you!