I'm using SPSiteDataQuery to query several lists in different sites. Unfortunately, I don't want the data from all of these sites; I have a list of ones to include and exclude.

So far I've seen SPSiteDataQuery.Webs which seems to only allow the scope to be changed (entire collection, just subsites, just this site), and SPSiteDataQuery. Lists which would require the List IDs (that I don't have) or the lists' template IDs (not specific enough).

How do I persuade SPSiteDataQuery to only return results for the sites on my include list? FYI: I have the IDs and URLs of the sites to include, and filtering post-fetch is not an option.

2 Answers 2


I don't think you can do this without some form of filtering post fetch. However, it isn't hard.

SPSiteDataQuery queries automatically include DataColumns in the DataTable for ListId and WebId, so you could then filter your DataTable by Web. I know it's not idea - 'query and filter', rather than just 'query' - but it should work.

The following queries for all documents with ID=1, and then filters to a particular Web. I guess your filter expression would be more complicated.

        using (SPSite site = new SPSite(url))
            using (SPWeb web = site.OpenWeb())
                SPSiteDataQuery q = new SPSiteDataQuery();
                q.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Integer'>1</Value></Eq></Where>";
                q.Webs = "<Webs Scope='SiteCollection' />";
                q.Lists = "<Lists BaseType=\"1\" />";
                q.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='FileLeafRef' />";

                DataTable table = web.GetSiteData(q);
                DataRow[] filteredRows = table.Select("WebId = '{282625A8-B13B-4DC0-B62E-D3255011FB5C}'");
                foreach (DataRow row in filteredRows)
                    foreach (DataColumn col in table.Columns)
                        string colData = string.Empty;
                        if (row[col] != null)
                            colData = row[col].ToString();
                        Console.WriteLine("{0} : {1}", col.ColumnName, colData);
  • 1
    Unfortunately complexity isn't the issue: When each site could contain up to 10000 items, post-fetch processing just isn't on the cards.
    – Stu Pegg
    Commented Apr 19, 2012 at 21:41
  • 1
    Then, sadly, I don't think it's possible using the SPSiteDataQuery.
    – Andy Burns
    Commented Apr 20, 2012 at 9:36
  • 1
    Fair enough. I'll leave this question open for a while, and then mark your answer if there are no alternatives offered.
    – Stu Pegg
    Commented Apr 20, 2012 at 9:52

You can add hidden field with SiteId to each list and use it in query. Dont forget to set data to this field in event recievers.

CrossListQueryInfo\SPSiteDataQuery how to skip items with specific WebId?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.