App builders frequently use SharePoint as a database. It's a popular choice because users with Microsoft 365 licenses can use SharePoint without any additional licensing.
The typical requirement that often arises is this - how do we move sites, lists, and data between different sites, or different SharePoint organisations?
The reason for this requirement is important because it supports the process of building apps against a DEV SharePoint site, and moving the changes to a LIVE site at the end of the process. It also supports the use-case where third parties build SharePoint based Power Apps for clients or customers.
Introducing the PnP Framework and Provisioning Engine
The PnP Framework and Provisioning Engine is exactly designed for this task (PnP standing for patterns and practices). With this method, we can define a SharePoint site, including lists, columns, content types, pages, and more, using a template. This template can be in XML, JSON, or a container format called a PnP file. We can then issue PowerShell commands to install the template using the Provisioning Engine.
The documentation here describes the PnP framework in a very readable way.
The PnP Framework and Provisioning Engine is open source, and a link to the github page is here:
For the rest of this post, we'll work on the basis that we've created a SharePoint site and that we've defined multiple lists with data. We'll create a PnP template based on our source site, and look at how to apply this template to a destination site.
How to install PnP PowerShell
To install PnP PowerShell, we start PowerShell as an administrator. That is, we search for PowerShell through the Windows 10 Start Menu, and we choose the option to 'run as an administrator'.
We type the following command to install PnP PowerShell:
Install-Module -Name PnP.PowerShell
During the installation process, we need to agree that the module comes from an untrusted repository. When we choose 'yes', the installer will download and install the PnP PowerShell.
Here's a screenshot of the process.
At the end of this process, we must run the following command to grant permissions to the PnP Management Shell.
Exporting/Downloading a SharePoint site definition
At this stage, we can run the PowerShell commands to export our source SharePoint site to a local file on our computer. Here are the PowerShell commands that we run:
#1 - Connect to the source SharePoint site
Connect-PnPOnline -Url "https://sourceSite.sharepoint.com/sites/sourceSite"
#2 - Export the source site definition
Get-PnPSiteTemplate -Out "C:\PowerApps\SharePoint\mySite.xml"
#3 - Export the list data for the specified lists
Add-PnPDataRowsToSiteTemplate -Path "C:\PowerApps\SharePoint\mySite.xml" -List "PropertyType"
Add-PnPDataRowsToSiteTemplate -Path "C:\PowerApps\SharePoint\mySite.xml" -List "Property"
The first cmdlet initiates the connection to our source SharePoint list. We would replace the URL with our source SharePoint address. PowerShell will open a dialog that prompts us to login using our Microsoft 365 account.
The next cmdlet (Get-PnPSiteTemplate) is the one that exports the site definition. We specify the destination on our local machine where we want to save the output template file.
Finally, we can optionally call the Add-PnPDataRowsToSiteTemplate cmdlet to specify the list data that we want to add to the template.
The documentation for the Get-PnPSiteTemplate cmdlet (and other cmdlets) are here:
It's also useful to note that the PnP framework has undergone occasional updates, and previous incarnations of these cmdlets were named differently. The one we use (Get-PnPSiteTemplate) is the latest version, whereas the previous version was called Get-PnPProvisioningTemplate. There are more details about the new and old names of cmdlets here:https://pnp.github.io/powershell/articles/upgrading.html
Examining the template fileAfter we export the template, we can open the XML template file in Visual Studio Code or a text editor. The example above exported the output template to "C:\PowerApps\SharePoint\mySite.xml".
The screenshot beneath highlights the XML from this template that defines a list of properties, and a section that defines a data record in this list. We can further customise our template manually by modifying the XML file.
Importing our changes to a destination SharePoint Site
To import our the SharePoint objects in the template file to a destination server/site, we must first create a site on our destination SharePoint server. We can then connect to this site by calling Connect-PnPOnline.
Here's a summary of the commands:
#1 - Connect to the destination SharePoint site
Connect-PnPOnline -Url "https://destinationSite.sharepoint.com/sites/destinationSite"
#2 - Import the items from the template file
Invoke-PnPSiteTemplate -Path "C:\PowerApps\SharePoint\mySite.xml"
When this process completes, we can navigate to our destination SharePoint site and view the lists and objects that we've added.
To copy SharePoint objects from a source site to a destination server/site, we can utilise the PnP Framework and Provisioning Engine. This post covered the steps and the PowerShell commands to carry out this task.