How to Create a Content Type in SharePoint using PowerShell?
A Content Type in SharePoint is a fundamental framework for any item, such as Task, Document, Event, etc. Every content type has its own set of metadata columns and templates, information management policies, and workflows. In SharePoint, we can create our own custom content types from existing content types, such as “Invoice” content type based on a document content type, and add custom columns to it, say “Invoice Date”.
How to Create a Content Type in SharePoint?
To add a content type in SharePoint, follow these steps:
- Go to Site Settings >> Click on “Site Content Types” under the “Web Designer Galleries” group.
- In Site content types page, You’ll see the list of default content types like Item, Tasks, Document, etc grouped by sections. Click on the “Create” link at the top.
- Provide a name for your custom content type. Optionally, you can enter the description for your new content type to make its purpose clear.
- Select the parent content type, such as “Item” from which your content type is based on. You can either create a new content type group or select any existing.
- Click OK to complete creating the content type. Once the content type is created, The next step is to add the required site columns to the content type to make metadata available from the content type.
Create Content Type in SharePoint using PowerShell
Let’s create a new content type in SharePoint 2016 using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$ContentTypeName = "Crescent Projects"
#Get the Root web
$web = Get-SPWeb $SiteURL
#Get parent content type - "Item"
$ParentContentType = $web.AvailableContentTypes["Item"]
#Check if the content type already exists
$ContentType = $web.ContentTypes[$ContentTypeName]
If($ContentType -eq $NULL)
{
#Create new content type from "item" Content Type
$ContentType = New-Object Microsoft.SharePoint.SPContentType($ParentContentType, $web.ContentTypes, $ContentTypeName)
$ContentType.Description ="Crescent Projects Content Type"
$ContentType.Group = "Crescent Content Types"
$web.ContentTypes.Add($ContentType)
$web.Update()
Write-Host "Content Type '$ContentTypeName' Created Successfully!" -ForegroundColor Green
}
else
{
Write-Host "Content Type '$ContentTypeName' Already Exists!" -ForegroundColor Red
}
This PowerShell creates a new content type in SharePoint. Now the next step is to add some columns to it: let’s add a “Project Name” column to the content type.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$ContentTypeName = "Crescent Projects"
#Get the Root web
$web = Get-SPWeb $SiteURL
#Get the Content Type
$ContentType = $web.ContentTypes[$ContentTypeName]
#Check if the content type exists
If($ContentType -ne $NULL)
{
#Create new Site column
$FieldName="ProjectName"
#Check if field exists already
$Field = $web.Fields | where {$_.InternalName -eq $FieldName}
If($Field -eq $null)
{
#Add a Site Column
$Field = $Web.Fields.Add($FieldName, [Microsoft.SharePoint.SPFieldType]::Text, $false)
}
#Get a Field Link to Add the site column to content type
$Field=$web.Fields[$FieldName]
$FieldLink = new-object Microsoft.SharePoint.SPFieldLink($Field)
#Add Site column to content type
$contentType.Fieldlinks.add($FieldLink)
$contentType.Update()
Write-Host "Fied Added to Content Type '$ContentTypeName' Successfully!" -ForegroundColor Green
}
else
{
Write-Host "Content Type '$ContentTypeName' Doesn't Exist!" -ForegroundColor Red
}
To create a content type in SharePoint Online using PowerShell, refer: How to Create a Content Type in SharePoint Online using PowerShell?