Friday, October 18, 2013

Sharepoint CAML query to fetch data from multiple Custom Lists - SPSiteDataQuery


Use SPSiteDataQuery  to fetch the data from multiple custom lists.

Code Examples are given below:

Retrieve data from Multiple Lists;

        SPSiteDataQuery query = new SPSiteDataQuery();
        query.Lists = "<Lists ServerTemplate=\"105\" />";
        query.Query = "<Where><Gt><FieldRef Name='colname'/>" +
"<Value Type='datatype'>somevalue</Value></Gt>
</Where>";
        DataTable dataTable = web.GetSiteData(query);
     
Fetch data from specific Lists;

       string  strListIDs += "<Lists>";
       strListIDs += "<List ID='GUID'/>";
       ListIDs += "<List ID='GUID'/>";
       strListIDs += "</Lists>";
       query.Lists = strListIDs ;
       query.ViewFields = "<FieldRef Name ='Title' />";
     DataTable dataTable = web.GetSiteData(query);

No comments:

Post a Comment