Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
[Parameter(Mandatory=$true, HelpMessage='Url for Site Collection hosting content type')]
[string]$Url = "",
[Parameter(Mandatory=$true, HelpMessage='Name of the Content Type to apply document template to')]
[string]$ContentTypeName = "Policy Document",
[Parameter(Mandatory=$true, HelpMessage='File path of the document template to upload')]
[string]$DocumentTemplatePath = "",
[Parameter(Mandatory=$false, HelpMessage='Filename to store the file in SharePoint as')]
[string]$DocumentTemplateFileName = "",
[Parameter(Mandatory=$false, HelpMessage='Should derived child content types and list content types be updated?')]
[boolean]$UpdateChildren = $true
$site=Get-SPSite $Url;
if($site -ne $null)
$rootWeb = Get-SPWeb $Url;
$contentType = $rootWeb.ContentTypes | ?{$_.Name -eq $ContentTypeName};
if($contentType -ne $null -and [System.IO.File]::Exists($DocumentTemplatePath))
$templateFile = [System.IO.File]::OpenRead($DocumentTemplatePath);
$memoryStream = new-object System.IO.MemoryStream;
$DocumentTemplateFileName = [System.IO.Path]::GetFileName($DocumentTemplatePath);
$destinationUrl=[String]::Format("{0}/{1}/{2}", $rootWeb.ServerRelativeUrl, $contentType.ResourceFolder.Url, $DocumentTemplateFileName);
if(-not [String]::IsNullOrEmpty($contentType.DocumentTemplateUrl))
Write-Host "Checking Content Type Document Template Url: $contentType.DocumentTemplateUrl for file" -ForegroundColor White;
$checkFile = $rootWeb.GetFile($contentType.DocumentTemplateUrl);
Write-Host "File found at $contentType.DocumentTemplateUrl, removing" -ForegroundColor White;
Write-Host "Checking for file @ $destinationUrl" -ForegroundColor White;
Write-Host "file found @ $destinationUrl, removing" -ForegroundColor White;
Write-Host "Content Type Document Template not set or found " $contentType.Name -ForegroundColor White;
$checkFile = $contentType.ResourceFolder.Files.Add($destinationUrl, $memoryStream.ToArray());
Write-Host "Uploaded file $DocumentTemplatePath -> $destinationUrl for" $contentType.Name -ForegroundColor White;
$contentType.DocumentTemplate = $DocumentTemplateFileName;
Write-Host "Applied Document Template $destinationUrl to " $contentType.Name " and updated content type (All Children Update Flag set? $UpdateChildren)" -ForegroundColor Green;
Write-Error "Cannot find $ContentTypeName in $Url or cannot find document template file $DocumentTemplatePath, please check.";
Write-Host "Cannot find Site $Url" -ForegroundColor Yellow;
Write-Host "Finished";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment