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

Share

Add Your Comments

Your email address will not be published. Required fields are marked *