0

I am trying to find out, how I can programatically (via powershell) modify "New" menu. I would like to add my own templates, change order and rename them. I can do it via UI, but I need to be able to do this on 300+ sites from time to time. I know, I could do like this, but I am searching for a solution, that would leverage what is shown below on the picture. Templates are stored in "Forms" folder, but I have no idea how to modify that menu.

Please, does anybody have an idea how to do this? i have no idea what to even search for :D

thank you

enter image description here

0

You can use the below PnP PowerShell code:

$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

Reference:

Create SharePoint Content Type with Document Template

Also, you can refer to the below PowerShell server object model code, easily you can convert this code into PowerShell CSOM:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


#Parameters
$SiteURL="http://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
}

Reference:

How to Change the Document Template in SharePoint Document Library?

Upload content type template through PowerShell

1 Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction:SilentlyContinue | Out-Null
 2 $url = "http://win-l2sfc3oetnn/sites/doctemp/" #ContentTypeHub Url
 3 $templateFolderPath = "C:\Projects\demos\TemplateDemo"
 4 $filter = "*.dotx", "*.potx", "*.vst", "*.xltx"
 5 
 6 $contentTypeFolders = ls $templateFolderPath | where {$_.PsIsContainer}
 7 $templateCount = (ls $templateFolderPath -Include $filter -Recurse | where {!$_.PsIsContainer}).Count
 8 $i = 1
 9 $fileMode = [System.IO.FileMode]::Open
10 
11 $contentTypeFolders | % {
12     $path = $_.FullName
13     $foldername = $_.Name
14     $ctindex = $foldername.LastIndexOf("0x")
15     
16     if($ctindex -gt 0) {
17     
18         $contenttypeid = $foldername.Substring($ctindex, $foldername.Length - $ctindex)
19         $spcontenttypeid = New-Object Microsoft.SharePoint.SPContentTypeId $contenttypeid
20         
21         ls -Path $path | % {
22             #Need to open a new spweb object, to "refresh" the current content type object, to aviod database update conflict
23             $web = Get-SPWeb $url
24             $spcontenttype = $web.ContentTypes[$spcontenttypeid]
25             $fileFullname = $_.FullName
26             $filename = $_.Name
27             
28             Write-Progress -PercentComplete (($i / $templateCount) * 100) -Activity "Uploading templates" -Status "$filename on content type $($spcontenttype.Name)"
29             
30             $i++
31             try{
32                 #open the document template, as a filestream
33                 $fileStream = New-Object "System.IO.FileStream" -ArgumentList $fileFullname, $fileMode
34                 #add the file (SPFile) to the content type resource folder http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttype.resourcefolder.aspx
35                 $spcontenttype.ResourceFolder.Files.Add($filename, $fileStream, $true) | Out-Null
36                 $fileStream.Close()
37                 #set the current document template file to be the document tmeplate
38                 $spcontenttype.DocumentTemplate = $filename
39                 #update the content type
40                 $spcontenttype.Update($true)
41             }
42             finally{
43                 $fileStream.Close()
44             }
45         }
46     }
47 }

Reference:

Upload content type template through PowerShell

In the above two different types of codes, the key code is below:

$site = Get-SPSite http://intranet.contoso.com/ ;
$web = $site.OpenWeb();
$spDocumentLibrary = $web.Lists["Shared Documents"];
$spDocumentLibrary.DocumentTemplateUrl = "NEW URL";
$spDocumentLibrary.Update();
| improve this answer | |
  • Thank you, but this approach creates multiple content types. If i were to have 50 templates I would end up with 50 content types. When I use that menu command, it doesn’t create a new content type. I have no idea how it is created. – Pedro May 7 at 17:08
  • Hi - then instead of using get-pnpcontenttype use the Get-PnPList, then directly you can assign the document template to the library itself which will avoid the content type. – Habibur Rahaman May 7 at 17:43
  • Or if you are going with normal powershell approach, you can use this code :$spDocumentLibrary = $web.Lists["Shared Documents"]; $spDocumentLibrary.DocumentTemplateUrl = "NEW URL"; – Habibur Rahaman May 7 at 17:45
  • Thank you. But this will only assign just one template. This menu allows you to attach as many templates as you wish - to a single content type – Pedro May 7 at 20:06
  • As per me the existing code you can use with the little modification... Keep all your template files in a local drive folder.. read this template file using for each loop and inside add above PnP content type based command, this way you are adding template file one by one to a single content type. – Habibur Rahaman May 8 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.