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();

            }

        }

    }

}

Note: – you can write query.ViewXml = string.Format(“<View Scope=’RecursiveAll’> <Query>{0}</Query></View>”, view.ViewQuery);

Here Scope=’RecursiveAll’ is used to retrieve item within folders and subfolders

Softree Consulting is an award-winning SharePoint consultancy that has been working with clients from across the globe to transform workplaces with tools and platforms from Microsoft technology. As a widely recognized organization, we are commonly approached to incorporate technical excellence and innovative design into all our customer actions.....

Site Footer