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 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:

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

  1. From the content type settings page, click on the “Advanced” link.
  2. Select “Upload a new document template”, Browse and select your document template.sharepoint online content type template
  3. Click on the “OK” button at 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 the 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 - 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 *