Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

SPSiteDataQuery.Webs property

Gets or sets the inner XML that specifies which Web sites to include in the query. as specified by the Scope attribute on the Webs tag in the query. By default, the query considers the current Web site, that is, the Web site from which the

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
public string Webs { get; set; }

Property value

Type: System.String
A string containing the inner XML that specifies the Web sites to include.

The Webs property specifies which Web sites to include in the query. By default, the query considers only the Web site from which the GetSiteData method was invoked.

You can broaden the scope of the query by setting the Webs property to a string containing a Webs tag and a Scope attribute. Possible values of the Scope attribute include Recursive and SiteCollection.

<Webs Scope="Recursive" />

<Webs Scope="SiteCollection" />

When the Scope attribute is set to Recursive, the query considers the current Web site and all subsites of the current Web site.

When the Scope attribute is set to SiteCollection, the query considers all Web sites that are in the same site collection as the current Web site. Of the two attribute values, this is the more inclusive.

The following example is a console application that retrieves information about the size of every file in every document library in a site collection. The application then iterates through the data and prints totals at the library, Web site, and site collection levels.

using System;
using System.Data;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("http://localhost"))
         {
            using (SPWeb web = site.OpenWeb())
            {
               SPSiteDataQuery query = new SPSiteDataQuery();

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

               // Ask for document libraries.
               query.Lists = "<Lists BaseType=\"1\" />";

               // Get IDs for fields used in the query.
               string fileSizeId = SPBuiltInFieldId.File_x0020_Size.ToString("B");
               string fileSizeDisplayId = SPBuiltInFieldId.FileSizeDisplay.ToString("B");

               // Select the records.
               query.Query = "<Where><Gt>";
               query.Query += "<FieldRef ID=\"" + fileSizeId + "\" />";
               query.Query += "<Value Type=\"Number\">0</Value>";
               query.Query += "</Gt></Where>";

               // Specify the view fields.
               query.ViewFields = "<FieldRef Name=\"" + fileSizeDisplayId + "\" />";
               query.ViewFields += "<ProjectProperty Name=\"Title\" />";
               query.ViewFields += "<ListProperty Name=\"Title\" />";

               // Submit the query.
               DataTable results = web.GetSiteData(query);

               // Process the results.
               string webTitle = string.Empty, listTitle = string.Empty;
               long listTotal = 0, webTotal = 0, grandTotal = 0;

               foreach (DataRow row in results.Rows)
               {
                  string curWeb = row["ProjectProperty.Title"] as string;
                  string curList = row["ListProperty.Title"] as string;

                  if (webTitle != curWeb)
                  {
                     listTotal = PrintSubTotal(listTotal, listTitle);
                     listTitle = string.Empty;

                     webTotal = PrintSubTotal(webTotal, webTitle);
                     webTitle = curWeb;

                     Console.WriteLine("\nWeb site: {0}", webTitle);
                  }

                  if (listTitle != curList)
                  {
                     listTotal = PrintSubTotal(listTotal, listTitle);
                     listTitle = curList;
                  }

                  // Get the size of the current file.
                  int fileSize = Convert.ToInt32(row[fileSizeDisplayId]);

                  // Add it to the totals.
                  listTotal += fileSize;
                  webTotal += fileSize;
                  grandTotal += fileSize;
               }

               listTotal = PrintSubTotal(listTotal, listTitle);
               webTotal = PrintSubTotal(webTotal, webTitle);
               Console.WriteLine("\nGrand total: {0}", grandTotal);

            }
         }
         Console.ReadLine();
      }

      private static long PrintSubTotal(long total, string name)
      {
         if (total != 0)
            Console.WriteLine("  Total file size for {0} is {1}.", name, total);
         return 0;
      }
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.