Clase ContentIterator
Proporciona métodos para recorrer en iteración los elementos de lista, listas, sitios para regular la cantidad de datos que se transfieren (es decir, para evitar que se produzca una SPQueryThrottledException).
Jerarquía de la herencia
System.Object
Microsoft.Office.Server.Utilities.ContentIterator
Microsoft.Office.Server.Utilities.TimerJobUtility
Espacio de nombres: Microsoft.Office.Server.Utilities
Ensamblado: Microsoft.Office.Server (en Microsoft.Office.Server.dll)
Sintaxis
'Declaración
Public Class ContentIterator
'Uso
Dim instance As ContentIterator
public class ContentIterator
Comentarios
Proporciona métodos auxiliares que aseguran que las listas se pueden consultar al regular la cantidad de datos que se transfieren. Esto es especialmente importante para queris en listas de gran tamaño o granjas de servidores Web para que la carga que se coloca en la base de datos no es excesiva.
Ejemplos
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);
}
);
}
}
}
Seguridad para subprocesos
Los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para subprocesos. No se garantiza que los miembros de instancias sean seguros para los subprocesos.