Partager via


ContentIterator - Classe

Fournit des méthodes pour itérer au sein des éléments de liste, des listes, des sites pour gérer la quantité de données transférées (par exemple, pour éviter de lever un SPQueryThrottledException).

Hiérarchie d’héritage

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

Espace de noms :  Microsoft.Office.Server.Utilities
Assembly :  Microsoft.Office.Server (dans Microsoft.Office.Server.dll)

Syntaxe

'Déclaration
Public Class ContentIterator
'Utilisation
Dim instance As ContentIterator
public class ContentIterator

Remarques

Fournit des méthodes d'assistance qui garantissent que les listes peuvent être interrogées lors de régulation de la quantité de données transférées. Ceci est particulièrement important pour les queris de longues listes ou des batteries de serveurs Web afin que la charge sur la base de données n'est pas excessive.

Exemples

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

Cohérence de thread

Tous les membres statique (Partagé dans Visual Basic)s publics de ce type sont thread-safe. Cela n’est pas garanti pour les membres d’instance.

Voir aussi

Référence

ContentIterator - Membres

Microsoft.Office.Server.Utilities - Espace de noms