Wednesday, 18 October 2017

Create, Retrieve Or Delete Site Content Type On SharePoint Using PnP PowerShell

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).
  1. #Get Current Context Site (Root)  
  2. $siteurl = ""  
  3. Connect-SPOnline -Url $siteurl  
  4. $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.
  1. function AddContentType(){  
  2.     Add-SPOContentType -Name "PnPContentType" -Description "PnP Content Type" -Group "PnPContentTypeGroup"  
  3. }  
  4. 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:
  1. function AddContentTypeUsingCT(){  
  2.     $pageCT = Get-SPOContentType -Identity Page  
  3.     Add-SPOContentType -Name "PnPPageContentType" -Description "PnP Content Type" -Group "PnPContentTypeGroup" -ParentContentType $pageCT  
  4. }  
  5. 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:
  1. function RetrieveContentTypes(){  
  2.     Get-SPOContentType        
  3. }  
  4. RetrieveContentTypes # Retrieves all the content types from site level  
The content type can be retrieved, using the content type Id.
  1. function RetrieveContentTypeByName(){  
  2.     Get-SPOContentType -Identity "Item"  
  3. }  
  4. 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.
  1. function RetrieveContentTypeById(){  
  2.     Get-SPOContentType -Identity 0x01  
  3. }  
  4. RetrieveContentTypeById # Retrieves the content type from site using id  
The site content types from the particular list can be retrieved, using the list name.
  1. function RetrieveSiteCTfromList(){  
  2.     Get-SPOContentType -List "PnPList"  
  3. }  
  4. 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.
  1. function RemoveContentType(){  
  2.     Remove-SPOContentType -Identity "PnPContentType" –Force  
  3. }  
  4. RemoveContentType # Delete the content type from site using name