3

I am working on a communication modern SharePoint site. and seems these modern sites, do not allow us to save the list as template. as i want to save our list as template and re-create it inside another site. so can anyone advice if there is a PnP script to do this task without having to allow custom script on the modern site or enable any feature?. as i do not want to modify the source site, either by enabling any features or allowing custom scripts.

Thanks

2

3 Answers 3

3

The best recommended approach is use the PnP Provisioning template.

  • Download the PnP Provisioning template using PnP

  • Filter the downloaded template (xml) because there is no way to generate only the specific list template, by default it will download all lists/libraries from the specific site.

  • Then create a new list using the filtered PnP template.

Sample code to get specif list from SharePoint Online for PnP Provisioning template:

$listName = "List Title for which want to generate template";
$outputTemplateFileName = "C:\Temp\PnP\SPListPnPProvisioning\ListTemplateDownloaded.xml";
$templates = Get-PnPProvisioningTemplate -OutputInstance -Handlers Lists
$singleListTemplate = $templates.Lists | Where-Object { $_.Title -eq $listName }
$templates.Lists.Clear()
$templates.Lists.Add($singleListTemplate)
Save-PnPProvisioningTemplate -InputInstance $templates -Out $outputTemplateFileName;

Write-host "Successfully PnP list template has been customized for a single list."

How to create a new list in the target site using the downloaded PnP template:

CLS
$userName = "[email protected]"
$passWord = "YourSPOPassword"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord

Connect-PnPOnline -Url "https://globalsharepoint2019.sharepoint.com/sites/SharePointRNDClassic" -Credentials $cred

Apply-PnPProvisioningTemplate -Path "C:\Temp\PnP\SPListPnPProvisioning\ListTemplate.xml"

Write-host "Successfully a list has been created in SharePoint online."

Demo Example:

Create list using PnP Provisioning template

Please read the below article where step by step it is explained how to do with the video demo:

Create SharePoint online list using PnP provisioning template

9
  • You can specify which list(s) to include... see my answer above :-) Commented Feb 21, 2020 at 12:38
  • @SP Expert i tried the Get-PnPProvisioningTemplate -Handlers Lists -ListsToExtract <GUID or Title> -Out .\template.xml and Apply-PnPProvisioningTemplate -Path .\template.xml..but on the second command i got this exception Apply-PnPProvisioningTemplate : Referenced content type 0x01***58 not available in site or in template now our list contain a custom content type, so could this be the reason?
    – John John
    Commented Feb 21, 2020 at 14:31
  • 1
    @SharePointTestDev - In my demo I have not used ListsToExtract <GUID or Title> parameter, I have used -Handlers Lists, this will give everything all lists, libraries and content types etc...using the demo approach we can filter this to fit our specific requirement....try to exclude the ListsToExtract <GUID or Title> parameter and see the result - how it works. :-)
    – SP 2022
    Commented Feb 21, 2020 at 15:46
  • 1
    @RuneSperre - Thanks for letting us know about the -ListsToExtract <GUID or Title> parameter, when I have explored about this it was not known to me, however, still I am not sure how does it work - I think it gives .pnp file rather the xml file as per the below MSDN command: Get-PnPProvisioningTemplate -Out template.pnp -ListsToExtract "Title of List One","95c4efd6-08f4-4c67-94ae-49d696ba1298","Title of List Three"
    – SP 2022
    Commented Feb 21, 2020 at 15:52
  • 1
    @RuneSperre oohk, thanks mate :-)
    – SP 2022
    Commented Feb 21, 2020 at 15:58
2

You can use Get-PnPProvisioningTemplate to export a list as a template and then recreate the list on another site using Apply-PnPProvisioningTemplate.

To export just a specific list you would use the following syntax:

Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/comsite
Get-PnPProvisioningTemplate -Handlers Lists -ListsToExtract <GUID or Title> -Out .\template.xml

To include fields and content types, modify -Handlers to:

-Handlers Lists, Fields, ContentTypes

The file template.xml can then be applied to another site using:

Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/anothersite
Apply-PnPProvisioningTemplate -Path .\template.xml
10
  • so the -out and -path will be a location inside my PC?
    – John John
    Commented Feb 21, 2020 at 13:57
  • 1
    Correct. In my example i used .\filename.xml which would be the current directory Commented Feb 21, 2020 at 13:58
  • and will this command by any mean affect the source list? i do not think so? is this correct?
    – John John
    Commented Feb 21, 2020 at 14:23
  • 1
    i tried your appraoch , but on the Apply i for this expectation Apply-PnPProvisioningTemplate : Referenced content type 0x01***58 not available in site or in template now our list contain a custom content type, so could this be the reason?
    – John John
    Commented Feb 21, 2020 at 14:30
  • It will not modify the source at all. To include handlers, you will need to include that as well in the handlers parameter. I will update the answer on how to do that. Commented Feb 21, 2020 at 14:50
0

You might also want to consider using a Site Design for this, especially if you want it to be repeatable easily - and to let site owners manage things themselves. You can use a Site Design at site provisioning time or at a later point to instantiate specific artifacts, like Content Types, lists/libraries, etc.

https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-design-overview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.