GitHub Icon Image
GitHub

Download all the content type document templates files associated with a library

Summary

The script will download all the document templates assigned to all content types in a library. I created this script as I needed to download the document templates assocaited to a library's content types and could not find as easy way to do it through the UI or did not want to download SharePoint Designer etc.

Implementation

  • Open Windows PowerShell ISE or VS Code
  • Copy script below to your clipboard
  • Paste script into your preferred editor
  • Change config variables to reflect the site, library name & download location required
  • PnP PowerShell
  • CLI for Microsoft 365

# SharePoint online site URL
$siteUrl = "https://contoso.sharepoint.com/sites/clientfacing"

# Display name of SharePoint document library
$libraryName = "Documents"

# Local path where document templates will be downloaded
$LocalPathForDownload = "c:\temp\"

# Connect to SharePoint online site
Connect-PnPOnline -Url $siteUrl -Interactive

# Get SharePoint list with content types
$list = Get-PnPList -Identity $libraryName -Includes ContentTypes

foreach ($CT in $list.ContentTypes | Where-Object {$_.ReadOnly -eq $false})
{
    if ($CT.DocumentTemplateUrl) {
        Write-Host "Downloading Document Template: $($CT.DocumentTemplate) for Content Type: $($CT.Name) to $LocalPathForDownload$($CT.DocumentTemplate)"

        # Download content type document template
        Get-PnPFile -Url $CT.DocumentTemplateUrl -Path $LocalPathForDownload -Filename $($CT.DocumentTemplate) -AsFile
    }
}

# Disconnect SharePoint online connection
Disconnect-PnPOnline

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


# SharePoint online site URL
$siteUrl = "https://contoso.sharepoint.com/sites/clientfacing"

# Display name of SharePoint document library
$libraryName = "Documents"

# Local path where document templates will be downloaded
$LocalPathForDownload = "c:\temp\"

# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
   m365 login
}

# Get SharePoint list content types
$contentTypes = m365 spo list contenttype list --webUrl $siteUrl --listTitle $libraryName | ConvertFrom-Json

foreach ($CT in $contentTypes | Where-Object {$_.ReadOnly -eq $false})
{
	if($CT.DocumentTemplateUrl) {
		Write-Host "Downloading Document Template: $($CT.DocumentTemplate) for Content Type: $($CT.Name) to $LocalPathForDownload$($CT.DocumentTemplate)"
		
		# Download content type document template
		m365 spo file get --webUrl $siteUrl --url $CT.DocumentTemplateUrl --asFile --path "$($LocalPathForDownload)\$($CT.DocumentTemplate)"
	}
}

# Disconnect SharePoint online connection
m365 logout

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

Contributors

Author(s)
Leon Armston
Ganesh Sanap

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI