Question

I have created a news post and saved it as template on a modern communication site. Is there an option to reuse the template while creating news posts on the subsites as well? Any pointers on this is appreciated. Thnaks

Was it helpful?

Solution

It is possible to just copy the Template files from /SitePages/Templates folder to all subsites.

However, there are some limitations

  • We must manually create at least one page template on the target subsite. This will trigger some internal process that will make tempaltes work. Otherwise, your copied templates will remain ignored. Good news is that you need to do it only once per subsite.
  • You need PowerShell PnP version: 3.12.19 or later

PowerShell PnP Code to copy all site templates from the root site to all subsite within site collection

    # Need new PnP version: 3.12.19 or later
    Install-Module SharePointPnPPowerShellOnline 

    # Make sure PnP version is 3.12.19 or later
    Get-Module SharePointPnPPowerShellOnline -ListAvailable


    # Ensure 'Templates' folder exists on all sub sites: 
    Get-PnPSubWebs -Recurse | % {Resolve-PnPFolder -SiteRelativePath "SitePages/Templates" -Web $_ }

    # Get All Page Templates:
    $PageTemplates = Get-PnPFolderItem -FolderSiteRelativeUrl '/SitePages/Templates'

    #Download all Page templates:
    $PageTemplates | % { Get-PnPFile -Url "/SitePages/Templates/$($_.Name)"  -Filename $_.Name -AsFile }

    # Upload Page templates to all subsites of the current site collection
    $subwebs = Get-PnPSubWebs -Recurse
    foreach ($subweb in $subwebs){
        Write-host Processing $subweb.Title
        Get-ChildItem -Filter "*.aspx" | % { Add-PnPFile -Web $subweb -Path "$(Get-Location)\$($_.Name)" -Folder "\SitePages\Templates" -Publish }           

        # TODO:
        # Subsite needs at least one Page tempalte saved manually. Otherwise our moved tempaltes won't show up.
    }

enter image description here

enter image description here

enter image description here

OTHER TIPS

The news post templates are stored in the current site "Site Pages" library, we cannot use this template on subsites.

In case someone wants to modify the above code to move templates to another site collection, try this :

$rootSite = "https://<site>.sharepoint.com"
$sourceContext = Connect-PnPOnline -Url $rootSite -UseWebLogin

# Get All Page Templates:
$PageTemplates = Get-PnPFolderItem -FolderSiteRelativeUrl '/SitePages/Templates'  
  -Connection $sourceContext

#Download all Page templates:
$PageTemplates | % { Get-PnPFile -Url "/SitePages/Templates/$($_.Name)" -Connection 
    $sourceContext  -Path c:\temp  -Filename $_.Name -AsFile }

$destSite = "https://<site>.sharepoint.com/sites/<site>"
$destContext = Connect-PnPOnline -Url $destSite -UseWebLogin

# Ensure 'Templates' folder exists on new Site Collection 
Resolve-PnPFolder -SiteRelativePath "SitePages/Templates" -Connection $destContext

# Upload Page templates to new site collection
$PageTemplates | % { Add-PnPFile -Connection $destContext -Path "C:\temp\$($_.Name)" - 
   Folder "SitePages\Templates" }

# Create one template to trigger the availability of all the migrated templates
# Assumes that you already the default page named 'Home.aspx' , which all new Site 
# Collection should have by default
$page = Get-PnPClientSidePage -Identity "Home.aspx"

# Save the page as template to be reused later-on
$page.SaveAsTemplate("Dummy_Template.aspx")

Remove-PnPFile -ServerRelativeUrl 
   "/sites/training/sitepages/templates/Dummy_Template.aspx" -Force

Variation to copy template to Subsite , no manual intervention required :

#Config Variables
$username="xxx"
$password ="xxx"
$encpassword = convertto-securestring -String $password -AsPlainText -Force
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist 
$username, $encpassword

$SiteURL = "https://<tenant>.sharepoint.com/Sites/<site>/<subsite-one>/"

$sourceContext = Connect-PnPOnline -Url $SiteURL -Credentials $creds

# Get All Page Templates
#  : I have a template created in here called 'Modern-Page-Template'
$PageTemplates = Get-PnPFolderItem -FolderSiteRelativeUrl '/SitePages/Templates' - 
Connection $sourceContext

#Download all Page templates :
$PageTemplates | % { Get-PnPFile -Url "/SitePages/Templates/$($_.Name)" -Connection 
$sourceContext  -Path c:\temp  -Filename $_.Name -AsFile }

$destSite = "https://<tenant>.sharepoint.com/sites/<site>/<subsite-two>/"
$destContext = Connect-PnPOnline -Url $destSite -Credentials $creds

# IF SUBSITE IS CLASSIC - you will need to create one Modern page
$page = Add-PnPClientSidePage  -Name "Dummy Page" -Connection $destContext

# Save this newly created modern page as template to force creation of the 'templates' 
#folder
$page.SaveAsTemplate("Dummy Page Template.aspx")

# Ensure 'Templates' folder exists on new Site Collection 
Resolve-PnPFolder -SiteRelativePath "SitePages/Templates" -Connection $destContext

# Upload Page templates to new site collection
$PageTemplates | % { Add-PnPFile -Connection $destContext -Path "C:\temp\$($_.Name)" - 
  Folder "SitePages\Templates" }

# delete the Dummy template as this is no longer required
Remove-PnPFile -ServerRelativeUrl "/sites/<site>/<subsite- 
 two>/sitepages/templates/Dummy Page Template.aspx" -Force

# Get the template from the template folder
$page = Get-PnPClientSidePage -Identity "Templates/Modern-Page-Template" -Connection 
  $destContext

# Save the page with a new name based on the template
$page.Save("welcome.aspx")
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top