How to Change the Document Template in SharePoint Document Library?

Requirement: Change document template in SharePoint document library.

How to Add New Document Template in SharePoint?

How do I add templates to a SharePoint document library? To add a document template to the SharePoint document library, we’ve to first upload a new document template to SharePoint and then change the document template URL of the SharePoint document library from Document Library Settings >> Advanced Settings.

add new document template sharepoint

PowerShell to upload a new document template to SharePoint

Here is how to add a template to a document library in SharePoint using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Parameters
$SiteURL="https://intranet.crescent.com"
$DocumentLibraryName="Invoices"
$DocumentTemplatePath="C:\Docs\Crescent Invoice Template V3.docx"

Try {
    #Get the Web and document Library
    $Web = Get-SPWeb $SiteURL
    $DocumentLibrary = $Web.Lists.TryGetList($DocumentLibraryName)
 
    #Check Cotent type and document template exists
    If($DocumentLibrary -ne $null -and [System.IO.File]::Exists($DocumentTemplatePath))
    {
        #Get the Template File
     $TemplateFile =(Get-ChildItem $DocumentTemplatePath).OpenRead()

        #Get the Template File Name
        $DocumentTemplateFileName = [System.IO.Path]::GetFileName($DocumentTemplatePath)

        #Upload the document Template 
        $DocumentTemplate = $DocumentLibrary.RootFolder.SubFolders["Forms"].Files.Add($DocumentTemplateFileName, $TemplateFile,$True)

        #Set Document Template of the Content type
     $DocumentLibrary.DocumentTemplateUrl = $DocumentTemplate.URL
        $DocumentLibrary.update()

        Write-Host -f Green "Added Document Template $DocumentTemplateFileName to $DocumentLibraryName"
    }
    Else
    {
        Write-host -f Yellow "Cannot find Document Library $DocumentLibraryName or document template file $DocumentTemplatePath!"
    }
}
Catch {
    write-host -f Red "Error Adding Template to Document Library!" $_.Exception.Message
}

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *