In this post, you will learn, how we can create, retrieve and delete the site content types on SharePoint sites, using PnP PowerShell. The Client Side Object Model is used internally for these operations. The update operation is not available for the content types.
Prerequisite:
You need to have PowerShell 3.0, available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation on the official site. The installers are available here.
The following operations will be compatible with SharePoint 2013 on-premise or Office 365 versions.
Connect To Site: Connect to the site, using the snippet, given below. The PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).
# Get Current Context Site (Root)
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$ctx = Get-SPOContext
Once connected, you can carry out any of the operations mentioned below, based on the requirement.
Create Site Content Type:
The content types can be added to the site collections by setting the context, using the site URL. PnP CSOM PowerShell can be used to complete the operation.
Add-SPOContentType command is used to create the site content types on SharePoint sites. The required parameters to create the new site content type are name, description and group. The new values can be passed as the parameters. In my example, I have created new site content called "PnPContentType". The default parent content type will be the item.
The following command snippet helps to create the new content type.
function AddContentType()
{
Add-SPOContentType -Name "PnPContentType" -Description "PnP Content Type" -Group "PnPContentTypeGroup"
}
AddContentType # Create New Content Type with name, description
The parent content type can be used as a base content type to create the new content types. The following example shows to create the new content type, using the page content type, given below:
function AddContentTypeUsingCT()
{
$pageCT = Get-SPOContentType -Identity Page
Add-SPOContentType -Name "PnPPageContentType" -Description "PnP Content Type" -Group "PnPContentTypeGroup" -ParentContentType $pageCT
}
AddContentTypeUsingCT # Create New Content Type with name, description using page content type
The following snapshot shows the new content types added to the site.
Retrieve Site Content Type: The content types available on the site can be retrieved by setting the content with the URL, using PnP PowerShell. The following snapshot shows retrieval of all the content types from the site: function RetrieveContentTypes()
{
Get-SPOContentType
}
RetrieveContentTypes # Retrieves all the content types from site level
The content type can be retrieved, using the content type Id. function RetrieveContentTypeByName()
{
Get-SPOContentType -Identity "Item"
}
RetrieveContentTypeByName # Retrieves the content type from site using name
The following snapshot shows the content type retrieval, using PnP command:
The content type can be retrieved using, the content type name. function RetrieveContentTypeById()
{
Get-SPOContentType -Identity 0x01
}
RetrieveContentTypeById # Retrieves the content type from site using id
The site content types from the particular list can be retrieved, using the list name. function RetrieveSiteCTfromList()
{
Get-SPOContentType -List "PnPList"
}
RetrieveSiteCTfromList # Retrieves the site content type from particular list
Delete Site Content Type: The site content type can be deleted from SharePoint site, using PnP PowerShell. Remove-SPOContentType is used to delete the content types. The content type name is passed for deleting the content type. The name or ID can be passed with the command, using the identity parameter. function RemoveContentType()
{
Remove-SPOContentType -Identity "PnPContentType" –Force
}
RemoveContentType # Delete the content type from site using name
Comments