Office 365 – Updating Content Type “DocumentTemplate” property using PowerShell

Welcome back to my blog.

Today I came across one query from the forum for updating the Content Type’s “DocumentTemplate” URL.  Here is the PowerShell Script to update the same.

PowerShell script uses Client Side Object Model to update the content type property.

 

Please change required in the script is specify the SharePoint Online URL, Content Type Name and DocumentTemplate URL.

cls
#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#region Input Variables

$SiteUrl = “https://xxx.sharepoint.com/sites/dev” #Replace the URL

$UserName = Read-Host -Prompt “Enter User Name”
$SecurePassword = Read-Host -Prompt “Enter password” -AsSecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword
#endregion
#region Connect to SharePoint Online tenant and Create Context using CSOM
Try
{
#region Load SharePoint Client Assemblies
Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll”
#endregion
#region connect/authenticate to SharePoint Online and get ClientContext object..
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$clientContext.Credentials = $credentials
Write-Host “Connected to SharePoint Online site: ” $SiteUrl -ForegroundColor Green
Write-Host “”
#endregion
}
Catch
{
$SPOConnectionException = $_.Exception.Message
Write-Host “”
Write-Host “Error:” $SPOConnectionException -ForegroundColor Red
Write-Host “”
Break
}
#endregion
if (!$clientContext.ServerObjectIsNull.Value)
{
$web = $clientContext.Web
$cts = $web.ContentTypes
$clientContext.Load($cts);
$clientContext.ExecuteQuery();

foreach ($ctype in $cts)
{
if($ctype.Name -eq “TestCT”) #Specify the Content Type Name
{

$ctype.DocumentTemplate = “” #specify the URL
$ctype.Update($true); #updates all children of the content type
$clientContext.Load($ctype);
$clientContext.ExecuteQuery();
Write-Host $ctype.Name “-” $ctype.DocumentTemplate
}
}

}

You can also download PowerShell Script

This entry was posted in SharePoint 2013. Bookmark the permalink.

Leave a Reply