Bulk Creation of SharePoint Pages and Adding Webparts
Blog

Bulk Creation of SharePoint Pages and Adding Webparts

By Roopa N  |  Published on June 2, 2022

Bulk Creation of SharePoint Pages and Adding Webparts

Introduction:

In this blog, we show you an example for Bulk page creations by adding webparts to it. As part of a migration project, this was the custom requirement on each site’s ‘Site Pages’ post modernization.

We found some issues continuing to exist in a Modern Page when it was migrated from the Classic Experience, where the Modernization scripts had been run and webparts were one of them.

In our case, we need to add a newly created SPFx webpart to a page where the Modernization of custom webpart from source to destination was not migrated.

$TenantUrl= "<Tenant URL>"

$CSVPath="<Path>.csv"

$AppName = "<SPFx App Name>"

$CRInfo_WebpartName="<Webpart Name>"

#Start
Connect-PnPOnline -Url  $TenantUrl -UseWebLogin

$data = Import-Csv -Path $CSVPath

#Fetch App details from Tenant 
$AppDt = Get-PnPApp -Scope Tenant | Where {$_.Title -eq $AppName}

if($AppDt.Id -ne "")
{
#Also making sure that if the App is not installed at each site collection      #level, it will be installed and then the page will be created for the webpart to be #added in the page.
 foreach ($row in $data) 
 { 
   Connect-PnPOnline -Url  $row.dstUrl  -UseWebLogin
  
  Get-PnPSite

  $App = Get-PnPApp | Where {$_.Title -eq $AppName}

  if($App.InstalledVersion.count -eq 0)
  { 
   Install-PnPApp -Identity $AppDt.Id
  }

  Add-PnPPage -Name "<PageName>.aspx" -LayoutType Article -Publish   
  
  $SitePages =Get-PnPListItem -List "Site Pages"

   ForEach($Page in $SitePages)
   {
     $PageNameWithExtension= ($Page.FieldValues.FileRef).split("/")[4]

     $PageNameWithExtension

    if($PageNameWithExtension -like '*<PageName>*')
    {
      Get-PnPClientSidePage -Identity "<PageName>.aspx"

       Write-Host $Page.FieldValues.FileRef "has been created"

       Add-PnPPageWebPart -Page <PageName> -Component $CRInfo_WebpartName 

       Write-Host "Webpart has been added to the page:" $Page.FieldValues.FileRef  
    }
   }    
  }     
} 

Parameters:

  • Tenant URL – Tenant URL of your SP online site
  • Path – Path to the csv file which contains all the site URLs
  • SPFx App Name – This is the SPFx webpart created app which needs to be installed to get our webpart in each site
  • Webpart Name – Name of the webpart that needs to be added on the newly created page

Below is how an input file can be prepared:

Once the above variables have been replaced according to your Tenant, from the above script we will be able to Create a new page with a given name and add webpart to the same in Bulk.

We hope you found this blog useful. We can put our decades of Digital Transformation experience and expertise to work for your organization. Please reach out to us know more.

Leave a comment

Your email address will not be published. Required fields are marked *

Unravel The Complex
Stay Connected

Subscribe and receive the latest insights

Netwoven Inc. - Microsoft Solutions Partner

Get involved by tagging Netwoven experiences using our official hashtag #UnravelTheComplex