Compartilhar via


ContentIterator Class

Provides methods to iterate list items, lists, sites to regulate the amount of data that is transferred (i.e., to avoid throwing a SPQueryThrottledException).

Inheritance Hierarchy

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

Namespace:  Microsoft.Office.Server.Utilities
Assembly:  Microsoft.Office.Server (in Microsoft.Office.Server.dll)

Syntax

'Declaration
Public Class ContentIterator
'Usage
Dim instance As ContentIterator
public class ContentIterator

Remarks

Provides helper methods that ensure lists can be queried while regulating the amount of data being transferred. This is especially important for queris on large lists or Web farms so that the load put on the database is not excessive.

Examples

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

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

ContentIterator Members

Microsoft.Office.Server.Utilities Namespace