top of page
Writer's pictureKasper Larsen

Document templates in SharePoint Libraries - part 2

Updated: Oct 15

Option B - use the view.NewDocumentTemplates


This is part 2 of a serie of blog posts about the options we have when dealing with Document Templates, part 1 is here.


Disclaimer: this blog post is about using document templates in an Enterprise/Provisioning scenario - not from an end user perspective.


Back in 2018 Microsoft changed the Out Of The Box document library to look as below:





Note the "+ Add template" option.


This will allow the end user to upload a document to the List and make it available as an option in the "New" dropdown.


The file being uploaded is actually saved in a hidden folder in the list called "Forms" alongside the usual NewForm, EditForm, AllItems etc.



Getting this setup to work in a provisioning engine took some work. You can find the source code at Add document templates to the New dropdown in a document library | PnP Samples



The short version is to upload the document template file to a specifc Url, then locate the default view on the library and inject section specifying:

  • the content type id

  • that it is an uploaded template

  • Template ID

  • Title

  • the URL to the template

  • and finally that it should be visble on the New dropdown


 $view = Get-PnPView -List $list -Connection $targetConn | Where-Object { $_.DefaultView -eq $true }
 $replacer = ',{"contentTypeId":"'+ $($ct.Id.StringValue) +'","isUpload":true,"templateId":"'+ $templateId +'","title":"'+ $templateId+'","url":"'+$targettemplateFileUrl+'","visible":true}'
    
    if($existingTemplates -ne $null)
    {
        $index = $existingTemplates.LastIndexOf("]")
        $baseNewTemplatesAsJson = $existingTemplates.Substring(0, $index) + "[ReplaceToken]]"
        
    }
    $newtemplatesAsJson = $baseNewTemplatesAsJson -replace "\[ReplaceToken\]", $replacer
    
    $view.NewDocumentTemplates = $newtemplatesAsJson
    $view.Update()
    Invoke-PnPQuery -Connection $targetConn


However, how does this approach work with a governance requirement that we should be able to update the document template with a minimum of hassle?


The most important parameter is the URL and I tried 3 approaches:

  1. uploading the template file to the /Forms hidden folder in the library and having the URL parameter point to the file in the /Forms folder.

    This is identical to the out of the box behavior when an enduser is manually is hitting "+ Add template" and it works fine.

  2. uploading the template file to another library on the site and having the URL parameter point to the file in this library

    This requires that the template library exists on the target site, but Site Assets is an obvious choice. This approach also works.

  3. uploading the template file to a library in the Content Type Hub and having the URL parameter point to the file there.

    I couldn't get this approach to work, neither with a full nor a server relative URL. It seems like the template file must be on the site.


So we are left with the first two options, and from a governance perspective both will require a script that will update each site where the document template file is used.

However, Option 2 will make it a lot easier to find the relevant sites as the content of Site Assets is searchable where the /Forms folder is not.

Option 2 will also be my prefered choice if we are deplaying several document libraries using these document templates on the same site. Just update the document template in Site Assets and all libraries will use the new template.




In the 3rd and last part of the series on Document templates we are going to look into using custom solutions.



70 views0 comments

Kommentarer


bottom of page