Sharepoint - Batch update via PNP Powershell

By default CSOM is batch update request so all the request are queued until execute query method is called.

Use Powershell SPO online commands to create new item but do not call execute query until you need it or until it reaches batch size. So for example

Below is sample script which runs on csv with two columns 'Title' and 'YesNo'. Tried and tested working fine. This will give you logic on how to implement this.

Below sample batches request for 3 items and then runs execute query. You can change if condition according to your requirement.

Add-Type -Path "C:\Siddharth\SPODLL\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Siddharth\SPODLL\Microsoft.SharePoint.Client.Runtime.dll"

$UserName = "[email protected]"
$Password = Read-Host -Prompt "Enter the password"    
$Url = "https://wayneenterprise.sharepoint.com/sites/justiceleague"


$context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
$list = $context.Web.Lists.GetByTitle("CustomList");
 $web = $Context.Web
$Context.Load($list)
$Context.Load($web)
$guid = $list.ID
$Context.ExecuteQuery()
#Write-Host $list.Title

$path = "C:\MYImportFolder\import.csv"
$index = 1;
Import-Csv $path -Header 'Title', 'YesNo' | Foreach-Object { 
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation 
$ListItem = $List.AddItem($ListItemInfo) 
#Set Column Values 
$ListItem["Title"] = $_.Title;
$ListItem["yes_x0020_no"] = $_.YesNo;
$ListItem.Update()
Write-Host $index;
if($index % 3 -eq 0) 
{
   Write-Host "divisible by 3";

   $Context.ExecuteQuery();
}
$index++;
}

$context.Dispose()

I don't think there is a way in PNP powershell to execute in batches currently.

You can implement techniques that can speed up your current script by implementing best practices for coding.

However , once i had this same issue and what I did was create multiple csv and run parallel threads of powershell to minimise the time .

It's not the best approach but it worked for me .


There is no way of batching PnP cmdlets. The Cmdlet runs and everything is "done" afterward.

Internally, PnP uses CSOM, which can be batched - up to a certain point (I am doing 200-300 delete operations or something like that but not too much).

If you really, really need batching, you can use the old SPWeb.ProcessBatchData-Method. However - it is SSOM!

I, myself, tend to write C# code, using CSOM and batch-update like so:

// open connection
// load "employees" in batches of 5000 using CAML and <RowLimit> into one single list "employeesToUpdate"
// modify all employees according to some "rules"
// now save:
var updates = 0;
foreach (var e in employeesToUpdate)
{
    e.Update();
    updates++;
    if (updates > 200)
    {
        logger.Info("Saving...");
        ctx.ExecuteQuery();
        updates = 0;
    }
}
if (updates > 0)
{
    logger.Info("Saving...");
    ctx.ExecuteQuery();
    updates = 0;
}

As you can see, above I chose to batch in packets of 200 - because 300 caused errors... I'm not sure how SharePoint determines how many "operations" can be made in one batch.