With the PnP PowerShell extensions for SharePoint Online it’s very easy to create a new content type by using the Add-PnPContentType cmdlet. But sometimes it is necessary to assign a new document template to the content type (or an existing one). This could be done using Client-site Object Model within the PowerShell Script as shown in the following script.


$filePath = ".\Document Template\DocumentTemplate.docx"
$filename = "DocumentTemplate.docx"

$serverRelativeSiteUrl = "/sites/my-demo"
$ctName = "Any new Document"

# create the content type
$ct = Add-PnPContentType -Name $ctName -ContentTypeId 0x0101006604da7f262243448cb56226f4f30c79 -Group "Tester" -Description "No description available."

# upload the document template to the corresponding folder of the content type (site relative url)
$f = Add-PnPFile -Path $filePath -Folder "/_cts/$ctName"

# get the content type object
$ct = Get-PnPContentType -Identity $ctName 

# set the document template in the content type to the uploaded file and update in the content database
$ct.DocumentTemplate = $filename
$ct.Update($true)
(Get-PnPContext).Load($ct)
Invoke-PnPQuery

# that's it

The result in the web view of the settings of the content type will look like this:

Keep it simple .

Advertisements