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
- Add reference “Microsoft.sharepoint.client.dll” and “Microsoft.sharepoint.client.Runtime.dll”.
- 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
Post a Comment