Un tutorial práctico para usar PowerShell y el módulo PnP para generar y aplicar plantillas de sitios de SharePoint.
SharePoint is a collaboration and content management platform that allows you to create websites for different purposes. Sometimes, it can be useful to create a template of an existing site that contains the structure, layout and data that we want to replicate on another site. To do this, we can use PowerShell and the SharePoint Online PnP module.
In this article, we are going to explain the steps to create a template of a SharePoint site containing a list of its elements, and then create a new site from that template. To do this, we need to have the latest versions of PowerShell (currently 7.4.2) and the PnP module SharePointPnPPowerShellOnline (currently 3.29.2101.0) installed.
Once we have checked that we have the corresponding versions installed, the steps to follow are as follows:
1. Connect to the source site, which is the one we want to use as the basis for the template.
Connect-PnPOnline -Interactive -URL “url del sitio que vamos a copiar”
This command will ask us to enter our login credentials for the site.
2. Once logged in, we move on to create the template.
Get-PnPSiteTemplate -Out PlantillaSP.xml
This command will generate an XML file containing the site’s information, such as pages, webparts, themes, etc. We can specify the path where we want the file to be saved.
3. Now we are going to add the desired list items to the template. In this case we will add the items from the NasaAPOD list. This command will add the data from the ‘NasaAPOD’ list to the template. We can specify the fields we want to include with the -Fields parameter.
Add-PnPDataRowsToSiteTemplate -Path “C:Users*****PlantillaSP.xml” -List ‘NasaAPOD’ -Fields ‘Fecha’, ‘Title, ‘Url Foto’, ‘Explicacion’
We perform this step with all the lists we want to include in the template with their elements..
4. The template is ready. Now we connect to the SharePoint domain where we want to create the new site.
Connect-PnPOnline -Interactive -URL “https://******.sharepoint.com/”
5. We create the new site, indicating the name and the url we want it to have. The -Lcid parameter indicates the language code of the site, in this case English.
New-PnPSite -Type CommunicationSite -Title “Nuevo Planetario” -Url “https://*****.sharepoint.com/sites/NuevoPlanetario” -Lcid 1033
6. We connect to the new site we have just created..
Connect-PnPOnline -Interactive -URL “https://****.sharepoint.com/sites/NuevoPlanetario”
7. We deactivate NoScript. This will allow us to apply the template to the new site, as by default SharePoint sites have the NoScript option enabled, which prevents page customisation.
Set-PnPTenantSite -Url “https://****.sharepoint.com/sites/NuevoPlanetario” -DenyAddAndCustomizePages:$false
8. We apply the template and our new SharePoint site is ready.
Invoke-PnPSiteTemplate -Path “C:/Users/*****/PlantillaSP.xml”
If when applying the template we get the error Invoke-PnPSiteTemplate: Access is denied (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)), it is because NoScript is not disabled. This may be because we did not follow all the steps correctly. To solve it we will repeat the steps from point 6, connect to the new site, deactivate NoScript and apply the template.
In the following image you can see all the steps indicated.
I hope you find it useful! : ) See you soon.