Export (0) Print
Expand All

SPSiteDataQuery.Lists Property

Windows SharePoint Services 3

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)

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.

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>
    

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 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;
      }
   }
}

Community Additions

ADD
Show:
© 2014 Microsoft