Wednesday, September 12, 2018

Fetch data from multiple lists using SPSITEDATAQUERY

I knew posting on SPSITEDATAQUERY to too old , But recently I came ac-crossed requirement to get data from multiple lists . But I am posting this to keep the query handy

SPSiteDataQuery query = new SPSiteDataQuery ();
  // Query all Web sites in this site collection.
               Query. Webs = "<Webs Scope=\"SiteCollection\">";

               //Ask for all lists created from the contacts template.
               Query. Lists = "<Lists ServerTemplate=\"105\" />";

               // Get the Title (Last Name) and First Name fields.
               query.ViewFields = "<FieldRef Name=\"Title\" />";
               query.ViewFields += "<FieldRef Name=\"FirstName\" Nullable=\"TRUE\"/>";

               DataTable results = web.GetSiteData (query);