kwizcom banner advertisement

Add Document Template to Content Type in SharePoint using PowerShell

Requirement:  Associate document template with content type in SharePoint

PowerShell to add document template to content type in SharePoint
Here is the PowerShell to upload document template to content type in SharePoint.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Parameters
$SiteURL="http://intranet.crescent.com"
$ContentTypeName="Crescent Invoice Template V2"
$DocumentTemplatePath="C:\Temp\Crescent Invoice Template V3.docx"

Try {
    #Get the Web and Content Type
    $Web = Get-SPWeb $SiteURL
    $ContentType = $Web.ContentTypes | Where {$_.Name -eq $ContentTypeName}
 
    #Check Cotent type and document template exists
    If($ContentType -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 = $ContentType.ResourceFolder.Files.Add($DocumentTemplateFileName, $TemplateFile,$True)

        #Set Document Template of the Content type
 $contentType.DocumentTemplate = $DocumentTemplateFileName
 $contentType.UpdateIncludingSealedAndReadOnly($True)
  
        Write-Host -f Green "Added Document Template $DocumentTemplateFileName to $ContentTypeName"
    }
    Else
    {
        Write-host -f Yellow "Cannot find Content Type $ContentTypeName or document template file $DocumentTemplatePath!"
    }
}
Catch {
    write-host -f Red "Error Adding Document Template to Content Type!" $_.Exception.Message
}
Add Document Template to Content Type in SharePoint using PowerShell Add Document Template to Content Type in SharePoint using PowerShell Reviewed by Salaudeen Rajack on February 07, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.