Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are on the right track and it is possible to retrieve results for lists in different webs. The following example shows how to retrieve items from the default 'Tasks' or 'Workflow Tasks' lists and works with task lists created at the root level and within a sub site.</p> <pre><code> SPSiteDataQuery q = new SPSiteDataQuery(); q.ViewFields = "&lt;FieldRef Name='Title'/&gt;&lt;FieldRef Name='Priority'/&gt;&lt;FieldRef Name='Status'/&gt;"; q.Webs = "&lt;Webs Scope='SiteCollection' /&gt;"; q.Lists = "&lt;Lists BaseType='0' /&gt;"; q.Query = "&lt;Where&gt;&lt;Gt&gt;&lt;FieldRef Name='ID' /&gt;&lt;Value Type='Number'&gt;0&lt;/Value&gt;&lt;/Gt&gt;&lt;/Where&gt;"; DataTable results = new DataTable(); using (SPSite site = new SPSite("http://sharepoint")) { using (SPWeb web = site.OpenWeb("subsite")) { results = web.GetSiteData(q); } } </code></pre> <p>I've written it using a hardcoded URL so you can run it inside a console application for testing but you can replace the using statements with something like <code>SPWeb web = SPContext.Current.Web;</code> when you put this inside a web part.</p> <p>A few other things worth considering:</p> <ul> <li>The lists you are querying must contain <strong>all</strong> the fields in the ViewFields element</li> <li>Multi-lookup fields do not work well with the SPSiteDataQuery (single value lookup fields are ok)</li> <li>The u2u CAML builder tool is also useful for testing CAML queries. See <a href="http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx" rel="nofollow noreferrer">http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload