How to get items from a specific SharePoint List View using client object model

Below code describes how to retrieve items from sharepoint list by specific view using client side object model
  1. Add reference “Microsoft.sharepoint.client.dll” and “Microsoft.sharepoint.client.Runtime.dll”.
  2. Write Below Code.
using Microsoft.SharePoint.Client;
namespace ConsoleApplication
{  
    class Program
    {
        static void main(string[] args)
        {
            using (ClientContext ctx=new ClientContext(“http://siteurl”))
            {
                //Apply Credential
                ctx.Credentials = new System.Net.NetworkCredential(“UserName”“Password”);
                Web web = ctx.Web;
                ctx.Load(web,w=>w.Lists);

                List list = web.Lists.GetByTitle(“New List”);//Load list
                ctx.Load(list, l => l.Views);
                ctx.ExecuteQuery();

                CamlQuery query = new CamlQuery();
                ListItemCollection itemColl = null;
                View view = list.Views.GetByTitle(“MyCustomView”);//enter view name

                ctx.Load(view, vw => vw.ViewQuery);
                ctx.ExecuteQuery();
                query.ViewXml = string.Format(“<View><Query>{0}</Query></View>”, view.ViewQuery);

                itemColl = list.GetItems(query);
                ctx.Load(itemColl);
                ctx.ExecuteQuery();
            }
        }
    }
}

Comments

Popular posts from this blog

Clearing the Configuration Cache for SharePoint

SharePoint 2013 REST API CRUD Operations

Add List Item Attachments to Task Form using Nintex Workflow and Forms