ContentIterator 类

提供方法来循环访问项列表、 列表、 网站来控制传输的数据量 (即,以避免引发SPQueryThrottledException)。

继承层次结构

System.Object
  Microsoft.Office.Server.Utilities.ContentIterator
    Microsoft.Office.Server.Utilities.TimerJobUtility

命名空间:  Microsoft.Office.Server.Utilities
程序集:  Microsoft.Office.Server(位于 Microsoft.Office.Server.dll 中)

语法

声明
Public Class ContentIterator
用法
Dim instance As ContentIterator
public class ContentIterator

备注

提供帮助器方法,以确保列表可以查询时,控制正在传输的数据量。这是对于大型列表或 Web 场的 queris 特别重要,以便将放在数据库上的负载不是过多。

示例

using System;

using Microsoft.SharePoint;
using Microsoft.Office.Server.Utilities;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace Microsoft.SDK.SharePointServer.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Put your web application URL here
            Uri webApplicationUrl = new Uri("https://localhost:80/");

            // Put your site Url here
            string siteUrl = "https://localhost:80/";

            SPWebApplication webApplication = SPWebApplication.Lookup(webApplicationUrl);

            ContentIteratorCodeSample1.ProcessAllSitesInWebApplication(webApplication);

            using (SPSite siteCollection = new SPSite(siteUrl))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    ContentIteratorCodeSample2.ProcessAllListItemsInSite(site);

                    FolderHierarchyCodeSample1.ProcessAllFoldersInList(site.Lists["Shared Documents"]);

                    string[] sharedDocumentsFolderUrls = new string[]
                    {
                        "/Shared Documents/folder1",
                        "/Shared Documents/folder2",
                        "/Shared Documents/folder3/subfolder1",
                    };

                    FolderHierarchyCodeSample2.ProcessFoldersFromList(site.Lists["Shared Documents"], sharedDocumentsFolderUrls);

                    string[] webFolderUrls = new string[]
                    {
                        "/Shared Documents/folder1",
                        "/Shared Documents/folder2",
                        "/Shared Documents/folder3/subfolder1",
                        "/Pages/folder1",
                        "/Pages/folder2",
                    };

                    FolderHierarchyCodeSample2.ProcessFoldersFromWeb(site, webFolderUrls);

                    Stream memoryStream = new MemoryStream(Guid.NewGuid().ToByteArray(), false);
                    FolderHierarchyCodeSample2.AddTemplateResource(siteCollection, memoryStream, Guid.NewGuid().ToString());
                }
            }
        }
    }
}

using System;
using System.Globalization;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.Office.Server.Utilities;

using ContentIterator = Microsoft.Office.Server.Utilities.ContentIterator;

namespace Microsoft.SDK.SharePointServer.Samples
{
    // This sample processes all list items in lists which have a Yes/No column named "ShouldProcess"
    public static class ContentIteratorCodeSample2
    {
        // Put your column name here
        private static string columnName = "ShouldProcess";

        // Replace this with your own query
        private static string CreateQuery()
        {
            // This example 
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.Append("<Where><Eq>");
            queryBuilder.AppendFormat(CultureInfo.InvariantCulture,
                "<FieldRef Name=\"{0}\" />",
                columnName);
            queryBuilder.Append("<Value Type=\"Boolean\">1</Value>");
            queryBuilder.Append("</Eq></Where>");
            queryBuilder.Append(ContentIterator.ItemEnumerationOrderByNVPField);

            return queryBuilder.ToString();
        }

        private static bool OnListException(SPList list, Exception e)
        {
            // Put your logic here to handle exceptions thrown while
            // processing a list.

            // Do not rethrow exception, keep iterating
            return false;
        }

        private static void ProcessItem(SPListItem item)
        {
            // Put your code here to process a list item
        }

        // This method iterates over every list item in a site that satifies a query
        public static void ProcessAllListItemsInSite(SPWeb site)
        {
            // Put your iterator name here which will be logged in the ULS logs
            string iteratorName = "Sample list item iterator";

            ContentIterator iterator = new ContentIterator(iteratorName);

            iterator.ProcessLists(site.Lists,
                delegate(SPList list)
                {
                    // Only process lists with which have the required column
                    if (!list.Fields.ContainsFieldWithStaticName(columnName))
                        return;

                    ContentIterator.EnsureFieldIndexed(list, list.Fields.GetFieldByInternalName(columnName).Id);

                    // Now iterate over all items in the list that satify the query
                    string query = CreateQuery();

                    ContentIterator itemsIterator = new ContentIterator(iterator);

                    // do a recursive query in all folders
                    itemsIterator.ProcessListItems(list,
                        query,
                        true,  
                        delegate(SPListItemCollection items)
                        {
                            // items is just a single page of results from the query
                            // so iterating over the collection directly is OK
                            foreach (SPListItem item in items)
                            {
                                ProcessItem(item);
                            }
                        },
                        null  // don't handle exceptions
                        );
                },
                delegate(SPList list, Exception e)
                {
                    return OnListException(list, e);
                }
            );
        }
    }
}

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

ContentIterator 成员

Microsoft.Office.Server.Utilities 命名空间