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.
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).
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.
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:
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:
The content type can be retrieved, using the content type Id.
The following snapshot shows the content type retrieval, using PnP command:
The content type can be retrieved using, the content type name.
The site content types from the particular list can be retrieved, using the list name.
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.