Powershell Sharepoint SPWeb too many items

150 Views Asked by At

I am trying to edit a large number of items (>400,000) on a sharepoint list using Powershell and GET-SPWeb. Ideally there would be a way to get only half or a quarter of the items at a time.

The following code worked up to 100.000 items, but throws

The following exception occurred while trying to enumerate the collection: Not enough storage is available to complete this operation (0x8007000e)

when trying to enter the foreach loop.

The server on which I am running this script locally has 64 GB of RAM.

$site = 'http://sharepointserver'
     
$SiteWebObject = Get-SPWeb $site
$SiteWebObjectList = $SiteWebObject.Lists["List"]
$SiteWebObjectListItems = $SiteWebObjectList.Items
    
$spQuery = New-Object Microsoft.SharePoint.SPQuery ($SiteWebObjectList["List"])

do {
    $listItems = $SiteWebObjectList.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
        
    foreach ($item in $SiteWebObjectListItems) {          
        # edit item    
    }           
}
while ($spQuery.ListItemCollectionPosition -ne $null)
1

There are 1 best solutions below

1
David On

I figured it out. The code below worked. I needed to add a RowLimit to fetch items in chunks.

$site = 'http://sharepointserver'
       
$SiteWebObject = Get-SPWeb $site
$SiteWebObjectList = $SiteWebObject.Lists["List"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='RecursiveAll'"
$spQuery.RowLimit = 50000

do {
    $listItems = $SiteWebObjectList.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
        
    foreach ($item in $listItems) {                
        # edit item
    }                                
}
while ($spQuery.ListItemCollectionPosition -ne $null)