636 Views
3 minute read
Categories
Content and Collaboration SharePoint Custom Development

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.

Roopa N

About Roopa N

Roopa holds 6.5 years of total experience and developed functionality to create the lists and libraries, sites, Site Columns, Content types, Site Definitions etc. pro grammatically in VS2010. She has expertise in developing custom web parts and also features to make the pages, lists, libraries to be visible based on user role. She customized lists, libraries, and created content types using OOB features. She developed Custom Web Parts, Features and Event Receivers using Visual Studio 20105) Used Ribbon interface to edit the pages.

Roopa was involved in Deployment is done as Farm and Sandboxed Solutions and handled the Admin part for the Application and also carry out a daily Activity - LOA - Lights On Activity which checks and maintains the Jobs and also Task Creations and I have handled the tasks of Re-creating the jobs manually.

Prior to joining Netwoven, Roopa was working as a SP Developer with in Wipro, Bangalore.

Roopa did her M.Tech from BITS Pilani University and B.Sc. from St. Annes Womens Degree College and is presently living in Bangalore with her family.

LinkedinTwitterFacebook

Leave a Reply

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




Enter Captcha Here :