Create a SharePoint Intranet

Batch App Creation

A PowerShell with SharePoint tutorial

In the previous demo, PowerShell icon Peter Kalmström showed how to create an empty Tasks app. If you also want to add content to the app, the process is rather slow, but in the demo below Peter shows how you can speed up the process with the PNP.PowerShell Batch functionality that comes with the PNP.Powershell library.

If you have not watched the first demo in this series, you should do that, because the code below does not work in PowerShell ISE.

A Batch is like a work order that contains all commands, or the "tasks" to do. When you use the Add-PnPListItem command with the -Batch parameter, nothing is really sent to SharePoint: The "task" is only added to the "work order". When the Batch contains all the commands, you need to invoke it. Then all the commands are executed at once, which is very fast.

In terms of client-server communication, the script only sends one command to the server when the invoke command is sent. If you instead use the Add-PnpIListItem many times without the Batch parameter, there is communication going back and forth between the client (your computer) and the SharePoint server, which is less effective.

For this demo, Peter uses a simple "Numbers" list with number-items from 1 to 100 and shows how much faster the items are created when you use the Batch functionality. In the next article, we will use a more realistic example to explain how to add a lot of data from an Excel sheet to a SharePoint list.

This script creates a "Numbers" list in the slower way:


Import-Module PnP.PowerShell
connect-pnponline https://m365x61537192.sharepoint.com/sites/Mark8ProjectTeam -Interactive
New-PnPList -Title "Numbers" -Template GenericList
for($i=0;$i-lt 100;$i++){
    Add-PnPListItem -List "Numbers" -Values @{"Title" = $i.ToString()
}


To use the script with the Batch functionality, Peter creates a variable called MyBatch for the number items. To create a new batch he uses the code: $MyBatch = New-PnPBatch

The action (for exampe additions, deletions or updates) must have a Batch parameter, in this case for the MyBatch variable. The last step is to Invoke this variable.


Import-Module PnP.PowerShell
connect-pnponline https://m365x61537192.sharepoint.com/sites/Mark8ProjectTeam -Interactive
New-PnPList -Title "Numbers" -Template GenericList
$MyBatch = New-PnPBatch
for($i=0;$i-lt 100;$i++){
	Add-PnPListItem -List "Numbers" -Values @{"Title" = $i.ToString() -Batch $MyBatch
}
Invoke-PnPBatch $MyBatch
						
back icon next icon
Products Buy FAQ Services Tips Books Contact About Us Tools

Security and integrity

Copyright  Kalmstrom Enterprises AB  All rights reserved