SharePoint Online: Create a Content Type with Document Template using PowerShell

Requirement: Add a new SharePoint Online content type with a document template

How to Create a Content Type with a Template in SharePoint Online?

Content Type is a Reusable component with a collection of related columns (or metadata). Say for e.g., “Invoice Template” will have relevant columns, such as: Invoice Number, Date, Customer Name, Total Amount etc. Content types may have Templates as well.

In SharePoint Online, you can create a content type with a document template. This allows you to easily create new documents that are formatted the same way every time. The template can be in any format that Office supports, such as Word, Excel, or PowerPoint. This blog post will show how to create a content type with a document template. We’ll also show how to use PowerShell to create a content type with a document template. This can be useful for automating the process of creating new content types and documents in SharePoint Online.

To add a content type with a template in SharePoint Online, follow these steps:

  • Click on Setting gear >> Go to Site Information >> View all site settings
  • Click on “Site Content Types” under “Web Designer Galleries” group.
  • In Site content types page, You’ll see the list of default content types like Item, Tasks, Document, etc. grouped by sections. Click on “Create” link at the top.sharepoint online content type document template
  • Provide a name to your custom content type. Optionally, you can enter the description for your new content type to make it clear.
  • Select the parent content type as “Document” to which your content type is based on. You can either create a new content type group or select any existing.
  • Click OK to complete adding content type. sharepoint online word template content type

Once the content type is created, The next step is to add the document template to it.

  • From the content type settings page, click on “Advanced” link.
  • Select “Upload a new document template”, Browse and select your document template.sharepoint online content type template
  • Click on “OK” button on the bottom to save all your changes. Publish the content type once from the content type settings page.

You can use this approach to create new documents based on a predefined template quickly.

It’s a good idea to create content types in the “Content Type Hub”! You can launch to the Content Type Hub by browsing to this URL: https://YourTenantUrl/sites/ContentTypeHub

PowerShell to Create a Content Type with Document Template

Now, let’s create a content type with a document template using PowerShell script in SharePoint Online.

#Parameters
$ContentTypeHubURL = "https://crescent.sharepoint.com/sites/contentTypeHub"
$ContentTypeName = "Crescent Invoice Template V2"
$TemplateFilePath = "C:\Temp\Document Template\Invoice-template.docx"
#content type template location - Server Relative URL
$TemplateFolder = "/Sites/ContentTypeHub/_cts/$ContentTypeName" 

Try { 
    #Connect to PnP Online
    Connect-PnPOnline -Url $ContentTypeHubURL -Interactive

    #Upload the document template to the corresponding folder of the content type - Site Relative URL
    $TemplateFile = Add-PnPFile -Path $TemplateFilePath -Folder $TemplateFolder
 
    #Create Content Type with Document Template
    Add-PnPContentType -Name $ContentTypeName -ParentContentType (Get-PnPContentType -Identity 0x0101) -DocumentTemplate $TemplateFile.ServerRelativeUrl
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}

How about updating an exciting content type with a new template?

#Parameters
$ContentTypeHubURL = "https://crescent.sharepoint.com/sites/contentTypeHub"
$ContentTypeName = "Crescent Invoice Template"
$TemplateFilePath = "C:\Temp\Document Template\Invoice-template-v2.docx"
#content type template location - Server Relative URL
$TemplateFolder = "/Sites/ContentTypeHub/_cts/$ContentTypeName" 

Try { 
    #Connect to PnP Online
    Connect-PnPOnline -Url $ContentTypeHubURL -Interactive

    #Upload the document template to the corresponding folder of the content type - Site Relative URL
    $TemplateFile = Add-PnPFile -Path $TemplateFilePath -Folder $TemplateFolder
 
    #Get the Content Type
    $ContentType = Get-PnPContentType -Identity $ContentTypeName

    #Update the document template for the content type
    $ContentType.DocumentTemplate = $TemplateFile.Name
    $ContentType.Update($true)
    Invoke-PnPQuery
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply