We will continue from the last post where we were doing operations on list using managed Client object model.Please read Post from Part -1 to Part -3 to go through all the examples of COM
Here we will retrieve the items from the list using CAML query.
Retrieve Items from List using CAML query:
Code Snippet:
using (ClientContext clientContext = new ClientContext(SiteURL)) { //To retrieve collection of items from the list based on CAML query. List lst = clientContext.Web.Lists.GetByTitle("NewList"); string titleName = "abc"; CamlQuery query = new CamlQuery(); query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + titleName + "</Value></Eq></Where></Query></View>"; ListItemCollection coll = lst.GetItems(query); clientContext.Load(coll); clientContext.ExecuteQuery(); } |
In this we are retrieving items based on the title field of the list. We can use any of the field of the list for filtering the results.
Retrieve all the items from the List/Documents library including folders and subfolders :
Objective is to retrieve all the items from the list including folders and subfolders.
Code Snippet :
//To retrieve all the items from a list/document library including folders and subfolders List lst1 = clientContext.Web.Lists.GetByTitle("NewList"); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = @"<View Scope='Recursive'/>"; ListItemCollection coll1 = lst1.GetItems(camlQuery); clientContext.Load(coll1); clientContext.ExecuteQuery(); |
Important :
camlQuery.ViewXml = @"<View Scope='Recursive'/>";
This query will retrieve all the items from the list.
Happy Coding J
Continue reading Part -5
No comments:
Post a Comment