GitHub Icon Image

Add Large List items to PnP Template

Summary

Add Large list items to PnP Template using PnP command Add-PnPDataRowsToSiteTemplate.

Implementation

  • Open Windows PowerShell ISE
  • Create a new file
  • Copy a script below,
  • PnP PowerShell

$url = "https://yourtenantname.sharepoint.com/sites/SiteCollection"
$listname = "YourLargeList"
$xmlFilePath="template.xml"
$BatchSize=2000
Connect-PnPOnline -Url $Url -Interactive
Try
{
  
  $clientContext = Get-PnPContext
  $targetWeb = Get-PnPWeb
  $targetList = $targetWeb.Lists.GetByTitle($listname)
  $clientContext.Load($targetList)
  $clientContext.ExecuteQuery()
  if ($targetList.ItemCount -gt 0){
  if ($targetList.ItemCount -le 5000){
      Add-PnPDataRowsToSiteTemplate -Path $xmlFilePath -List $targetList -Query '<View></View>'
  }
  else
  {
  $loopCount =[math]::ceiling($targetList.ItemCount/$BatchSize)
  $startCount = 0
  $initialStartCount = 1
  $endCount = $BatchSize
      for ($count = 0;$count -lt $loopCount;$count++)
      {If($count -eq $loopCount-1){
              $templatequery = '<Query><Where><And><Gt><FieldRef Name=""ID""></FieldRef><Value Type=""Number"">' +$startCount + '</Value></Gt><Lt><FieldRef Name=""ID""></FieldRef><Value Type=""Number"">' +$endCount + '</Value></Lt></And></Where></Query>'
                  Add-PnPDataRowsToSiteTemplate -Path $xmlFilePath -List $targetList -Query $templatequery
          }
          else{
              $camlQuery = ""<View><Query><Where><And><Gt><FieldRef Name='ID'></FieldRef><Value Type='Number'>$startCount</Value></Gt><Lt><FieldRef Name='ID'></FieldRef><Value Type='Number'>$endCount</Value></Lt></And></Where><View><OrderBy><FieldRef Name='ID' Ascending='True' /></OrderBy></View></Query></View>""
              $Items = Get-PnPListItem -List $targetList -Query $camlQuery | select -Last 1
              $templatequery = '<Query><Where><And><Gt><FieldRef Name=""ID""></FieldRef><Value Type=""Number"">' +$startCount + '</Value></Gt><Lt><FieldRef Name=""ID""></FieldRef><Value Type=""Number"">' +$endCount + '</Value></Lt></And></Where></Query>'
              Add-PnPDataRowsToSiteTemplate -Path $xmlFilePath -List $targetList -Query $templatequery
              $startCount = $initialStartCount + $Items.Id
              $endCount = $endCount + $BatchSize
              }
          }
      }
  }
}
Catch {}
Disconnect-PnPOnline

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

Contributors

Author(s)
Jiten Parmar

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI