I want to create the content type using power shell. By using created content type with need to create the content type template using power shell script.
1 Answer
Using the below PnP PowerShell, we can create the content type in SharePoint online and can assign a document template to it:
$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
Demo output:
Reference:
Create SharePoint Content Type with Document Template
Other ways of creating content type - using PowerShell:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#function to turn ON Content Type in SharePoint Online list or library
Function Create-SPOContentType()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $CTypeName,
[Parameter(Mandatory=$true)] [string] $CTypeDesc,
[Parameter(Mandatory=$true)] [string] $ParentCTypeName,
[Parameter(Mandatory=$true)] [string] $CTypeGroup
)
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get all content types from the site
$ContentTypeColl = $Ctx.web.ContentTypes
$Ctx.Load($ContentTypeColl)
$Ctx.ExecuteQuery()
#Get the parent content type
$ParentCType = $ContentTypeColl| Where {$_.Name -eq $ParentCTypeName}
#Check if content type exists already
$ContentType = $ContentTypeColl| Where {$_.Name -eq $CTypeName}
If($ContentType -ne $Null)
{
Write-host "Content type '$CTypeName' already exists!" -ForegroundColor Yellow
}
else
{
#Specify properties for the new content type
$CTypeCreationInfo=New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
$CTypeCreationInfo.Name=$CTypeName
$CTypeCreationInfo.Description=$CTypeDesc
$CTypeCreationInfo.Group=$CTypeGroup
$CTypeCreationInfo.ParentContentType=$ParentCType
# sharepoint online powershell create content type
$ContentType=$ContentTypeColl.Add($CTypeCreationInfo)
$Ctx.ExecuteQuery()
Write-host "Content Type '$CTypeName' Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Content Type!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$CTypeName="Projects"
$CTypeDesc="Content type for Project template"
$ParentCTypeName="Item"
$CTypeGroup="Crescent Projects"
#Call the function
Create-SPOContentType -SiteURL $SiteURL -CTypeName $CTypeName -CTypeDesc $CTypeDesc -ParentCTypeName $ParentCTypeName -CTypeGroup $CTypeGroup
The above same approach Create Content Type in SharePoint Online using PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ContentTypeName ="Crescent Projects V3"
$ContentTypeDescription ="Base Content Type for Crescent Projects Template"
$ContentTypeGroupName = "Crescent Content Types"
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Create Content Type
Add-PnPContentType -Name $ContentTypeName -Description $ContentTypeDescription -Group
$ContentTypeGroupName
Reference:
SharePoint Online: Create a Content Type using PowerShell
Update:
How to add template to Content Type from the local system?
function Set-SPODocumentTemplate($ctx, $ctname, $templateName, $templatePath){
write-host "Getting $($ctname) content type information... " -NoNewline
$contentTypes = $ctx.Site.RootWeb.ContentTypes
$folder = $ctx.Site.RootWeb.GetFolderByServerRelativeUrl("_cts/$($ctname)")
$ctx.Load($folder)
$ctx.Load($contentTypes)
try{
$ctx.executeQuery()
write-host " done." -ForegroundColor Green
}
catch{
write-host "Error While Getting $($ctname) content type information $($_.Exception.Message)" -foregroundcolor red
}
write-host "Uploading $($templateName) document template... " -NoNewline
$FileStream = New-Object IO.FileStream($templatePath,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $templateName
$Upload = $folder.Files.Add($FileCreationInfo)
$contentType = $contentTypes | Where {$_.Name -eq $ctname}
$ctx.Load($contentType)
try{
$ctx.executeQuery()
write-host " done." -ForegroundColor Green
}
catch{
write-host "Error While Uploading $($templateName) document template $($_.Exception.Message)" -foregroundcolor red
}
write-host "Setting $($ctname) content type document template... " -NoNewline
$contentType.DocumentTemplate = $templateName
$contentType.Update($true)
try{
$ctx.executeQuery()
write-host " done." -ForegroundColor Green
}
catch{
write-host "Error While Setting $($ctname) content type document template $($_.Exception.Message)" -foregroundcolor red
}
}
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"
$siteUrl = Read-Host -Prompt "Provide the URL"
$adminUsername = Read-Host -Prompt ("Provide user for {0}" -f $siteUrl)
$secureAdminPassword = Read-Host -Prompt ("Provide password for {0}" -f $adminUsername) -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adminUsername, $secureAdminPassword)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = $credentials
Set-SPODocumentTemplate $ctx "JS231SP Word Document" "template.docx" "C:\Templates\template.docx"
Set-SPODocumentTemplate $ctx "JS231SP Excel SpreadSheed" "template.xlsx" "C:\Templates\template.xlsx"
Set-SPODocumentTemplate $ctx "JS231SP PowerPoint Presentation" "template.pptx" "C:\Templates\template.pptx"
Reference:
Setting the document template for a content type in SharePoint Online with PowerShell
-
Content type able to create. but i want to add the template from local machine not with existing document templateLiz– Liz2020-06-09 07:28:40 +00:00Commented Jun 9, 2020 at 7:28
-
Anser updated - please see the "How to add template to Content Type from the local system? "SP 2022– SP 20222020-06-09 07:47:38 +00:00Commented Jun 9, 2020 at 7:47
-
i am getting error in $f = Add-PnPFile -Path $filePath -Folder "/_cts/$ctName"Liz– Liz2020-06-09 09:06:22 +00:00Commented Jun 9, 2020 at 9:06
-
-
Try to pass the file path as $filePath = "Your local drive template file location path" or you can use the last PowerShell function function Set-SPODocumentTemplate($ctx, $ctname, $templateName, $templatePath) to add the template to the content type.SP 2022– SP 20222020-06-09 13:03:28 +00:00Commented Jun 9, 2020 at 13:03
