Condividi tramite


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)

Syntax

Public Property Webs As String
    Get
    Set

Dim instance As SPSiteDataQuery
Dim value As String

value = instance.Webs

instance.Webs = value
public string Webs { get; set; }

Property Value

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

Remarks

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.

Examples

The following example is a console application that retrieves data from a specific list. The query will succeed if the list exists anywhere within the current site collection because the Scope attribute of the Webs tag is set to SiteCollection.

Imports System
Imports System.Data
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("http://localhost")
         Using web As SPWeb = site.OpenWeb("/subsite/")

            Dim query As SPSiteDataQuery = New SPSiteDataQuery()

            ' Ask for a list by ID.
            query.Lists = "<Lists><List ID='b0b0bf7f-a2ca-43cc-b63e-a74ba8db93db'/></Lists>"

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

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

            Dim results As DataTable = web.GetSiteData(query)

            Dim firstName As String
            Dim i As Integer
            For i = 0 To results.Rows.Count - 1 Step i + 1
               firstName = results.Rows(i)("FirstName")
               Console.WriteLine("{0}, {1}", results.Rows(i)("Title"), _
                                 IIf(String.IsNullOrEmpty(firstName), "unknown", firstName))
            Next

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module
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("/subsite/"))
            {
               SPSiteDataQuery query = new SPSiteDataQuery();

               //Ask for a list by ID.
               query.Lists = "<Lists><List ID=\"b0b0bf7f-a2ca-43cc-b63e-a74ba8db93db\"/></Lists>";

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

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

               DataTable results = web.GetSiteData(query);

               string firstName;
               for (int i = 0; i < results.Rows.Count; i++)
               {
                  firstName = results.Rows[i]["FirstName"] as string;
                  Console.WriteLine("{0}, {1}", results.Rows[i]["Title"], 
                                    string.IsNullOrEmpty(firstName) ? "unknown" : firstName );
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

See Also

Reference

SPSiteDataQuery Class

SPSiteDataQuery Members

Microsoft.SharePoint Namespace