次の方法で共有


SPSiteDataQuery.Lists Property

Gets or sets the inner XML that specifies which lists to include in the query.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

Public Property Lists As String
    Get
    Set

Dim instance As SPSiteDataQuery
Dim value As String

value = instance.Lists

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

Property Value

Type: System.String
An XML string that contains either an integer ID that specifies the list type, or GUIDs that specify the IDs of lists.

Remarks

The top-level element in the string must be Lists.

Lists Attributes

Supported optional attributes for the Lists tag include the following:

  • ServerTemplate -- Limits the query to lists of the specified server template. Example: <Lists ServerTemplate="104" />

  • BaseType -- Limits the query to lists of the specified base type. Example: <Lists BaseType="1" />

    The following table lists possible values for the default base types.

    Value

    Description

    0

    Generic list

    1

    Document library

    3

    Discussion forum

    4

    Vote or Survey

    5

    Issues list

  • Hidden -- Determines whether the query will include hidden lists. Example: <Lists Hidden = "TRUE />

    By default, the query will consider all non-hidden lists of base type 0.

  • MaxListLimit -- Limits the query to the total number of lists specified. If the query would exceed the limit, the query will instead fail and raise an SPException. By default, the limit is 1000. When set to 0, there is no limit to the number of lists that will be considered. Example: <Lists MaxListLimit="500" />

Lists Subelements

Possible subelements of the Lists tag include List and WithIndex.

  • The List tag allows the query to include specific lists, instead of returning all lists of a particular type. The ID attribute identifies each list. Example:

    <Lists>
      <List ID="7A9FDBE6-0841-430a-8D9A-53355801B5D5" />
      <List ID="3D18F506-FCA1-451e-B645-2D720DC84FD8" />
    </Lists>
    
  • The WithIndex tag is an optional child of Lists and, when present, the query will be limited to lists with indexed fields. The WithIndex element has three required attributes: FieldId, Value, and Type. The Type attribute must be set to Text. In the following example, the query considers only lists that contain items whose specified field is set to the text value “Complete”.

    <Lists>
      <WithIndex FieldId="D4819257-6B69-41F1-82C8-A91615BFF500" Type="Text" Value="Complete" />
    </Lists>
    

Examples

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.

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()

            Dim query As SPSiteDataQuery = New SPSiteDataQuery()

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

            ' Ask for document libraries.
            query.Lists = "<Lists ServerTemplate='101' />"

            ' Select the records.
            query.Query = "<Where>" + _
                             "<Gt>" + _
                               "<FieldRef Name='File_x0020_Size' />" + _
                               "<Value Type='Number'>0</Value>" + _
                             "</Gt>" + _
                          "</Where>"

            ' Specify the fields.
            query.ViewFields = "<FieldRef Name='FileSizeDisplay' />" + _
                               "<ProjectProperty Name='Title' />" + _
                               "<ListProperty Name='Title' />"

            ' Submit the query.
            Dim results As DataTable = web.GetSiteData(query)

            ' Process the results.
            Dim webTitle As String = String.Empty
            Dim listTitle As String = String.Empty
            Dim listTotal As Long = 0
            Dim webTotal As Long = 0
            Dim grandTotal As Long = 0

            Dim i As Integer
            For i = 0 To results.Rows.Count - 1 Step i + 1

               Dim curRow As DataRow = results.Rows(i)
               Dim curWeb As String = CType(curRow("ProjectProperty.Title"), String)
               Dim curList As String = CType(curRow("ListProperty.Title"), String)

               If webTitle <> curWeb Then
                  listTotal = PrintSubTotal(listTotal, listTitle)
                  listTitle = String.Empty

                  webTotal = PrintSubTotal(webTotal, webTitle)
                  webTitle = curWeb

                  Console.WriteLine(vbCrLf + "Web site: {0}", webTitle)
               End If

               If listTitle <> curList Then
                  listTotal = PrintSubTotal(listTotal, listTitle)
                  listTitle = curList
               End If

               ' Get the size of the current file.
               Dim fileSize As Integer = Convert.ToInt32(results.Rows(i)("FileSizeDisplay"))

               ' Add it to the totals.
               listTotal += fileSize
               webTotal += fileSize
               grandTotal += fileSize

            Next

            listTotal = PrintSubTotal(listTotal, listTitle)
            webTotal = PrintSubTotal(webTotal, webTitle)
            Console.WriteLine(vbCrLf + "Grand total: {0}", grandTotal)

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub

   Function PrintSubTotal(ByVal total As Long, ByVal name As String) As Long
      If total <> 0 Then
         Console.WriteLine("  Total file size for {0} is {1}.", name, total)
      End If
      Return 0
   End Function

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())
            {
               SPSiteDataQuery query = new SPSiteDataQuery();
               
               // Query all Web sites in this site collection.
               query.Webs = "<Webs Scope=\"SiteCollection\">";

               // Ask for document libraries.
               query.Lists = "<Lists ServerTemplate=\"101\" />";

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

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

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

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

               for (int i = 0; i < results.Rows.Count; i++)
               {
                  DataRow curRow = results.Rows[i];
                  string curWeb = curRow["ProjectProperty.Title"] as string;
                  string curList = curRow["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(results.Rows[i]["FileSizeDisplay"]);

                  // 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.Write("\nPress ENTER to continue...");
         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;
      }
   }
}

See Also

Reference

SPSiteDataQuery Class

SPSiteDataQuery Members

Microsoft.SharePoint Namespace