Running Content Iterator in PowerShell

PowerShell can be very useful to quickly write up a script when you need to say update a field value for items in a list.

But what happens if your list happens to have 100,000+ items? Doing SPList.Items is going to get throttled.

And while PowerShell doesn’t have an equivalent for the ContentIterator class (which can iterate through large data sets while avoiding throwing a SPQueryThrottledException), we can run custom code through PowerShell that¬†utilizes ContentIterator.

The script below does a simple field update for items of a specific content type in a list.

param ( 
$Assemblies = ("Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c",  
               "Microsoft.Office.Server, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c")  
$Source = @" 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Administration; 
using Microsoft.Office.Server.Utilities; 
using System; 
namespace ContentIterator.Example 
    public static class FieldUpdater 
        public static void Update(SPList list) 
            SPContentType contentType = list.ContentTypes["Example Content Type"]; 
            string query = @" 
                        <FieldRef Name='ContentTypeId' /> 
                        <Value Type='Text'>" + contentType.Id + @"</Value> 
            ContentIterator iterator = new ContentIterator(); 
                delegate(SPListItemCollection items) 
                    foreach (SPListItem item in items) 
                delegate(SPListItemCollection items, Exception ex)  
                    throw ex; 
        private static void UpdateItem(SPListItem item)  
            if (item.Fields.ContainsField("CustomField"))  
                item["CustomField"] = "Some other value";
try { 
    Add-Type -ReferencedAssemblies $Assemblies -TypeDefinition $Source -Language CSharp 
    $web = Get-SPWeb $url 
    if (!$web.Exists) { 
        throw [Exception] ($url + " does not exist.") 
    $site = $web.Site 
    $rootWeb = $site.RootWeb 
    $list = $web.Lists.TryGetList($listName) 
    if ($list -eq $null) { 
        throw [Exception] ($listName + "does not exist in " + $url + ".") 
    if (!$list.Fields.ContainsField("CustomField")) { 
        throw [Exception] ($listName + "does not have CustomField.") 
    Write-Host "Indexing Content Type ID field..." -NoNewLine 
    $field = $list.Fields.GetField("ContentTypeId") 
    if ($field.Indexed) { 
        Write-Host "Skipped" -ForegroundColor Yellow 
    else { 
        $field.Indexed = $true 
        Write-Host "Done" -ForegroundColor Green 
    Write-Host "Updating CustomField..." -NoNewLine 
    Write-Host "Done" -ForegroundColor Green 
catch { 
    Write-Host "Error" -ForegroundColor Red 
    Write-Host $_.Exception.Message -ForegroundColor Red 
    Write-Host $_.Exception.StackTrace -ForegroundColor Red 

Note that we also indexed the ContentTypeId field as we’re using that field to query into the list.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s