Create and assign Content Types to Libraries using PowerShell PnP

The way you use PowerShell for SharePoint Online (SPO) is a bit different than with SharePoint Server.
You don’t have access to the SPO servers, and you have way less cmdlets available compared to SharePoint Server.

But thanks to very nice people out there, we can now use SharePoint PnP (Patterns & Practices) to create or retrieve things easily.
For more info on PnP, please visit the official documentation.

 

Download & install the SharePoint PnP module

Before you can use the SharePoint PnP cmdlets, you need to download and install the module.
This is available on GitHub – SharePoint PnP PowerShell.

 

Connect to the SPO site

Once the PnP module is installed, we need to connect first to the site collection, where you wish to create the Content Type (CT).

Connect-PnPOnline -Url https://<tenant>.sharepoint.com/sites/newteamsite -Credentials (Get-Credential)
ConnectPnPOnline

Connect-PnPOnline

Note that the cmdlets start with “PnP”

 

Get the Parent Content Type

If we want to create a content type, where the parent content type is “Document” (like when creating via the interface), we need to retrieve the info for that Document CT.

Get-PnPContentType
GetPnPCT

Get-PnPContentType

And let’s store this in a variable

$ParentCT = Get-PnPContentType -Identity Document

 

 

Create the content type

Now it’s time to create the content type itself to the chosen SharePoint site.

Add-PnPContentType -Name "MyNewCT" -Description "New CT created with PnP" -Group "MyCustomCTGroupPnp" -ParentContentType $ParentCT
NewCTCreated

New Content Type created

 

 

 

 

 

  • Navigate to SPO
  • Go to Site settings >> Site content types

And check out the newly created content type!

NewCTinSPO

New content type in SPO

Note:
If your group didn’t exist prior to running the cmdlet, PnP will create it for you.
If you don’t specify a parent, the “ParentContentType” will be the item itself.

 

Assign the new Content Type to a Library

The goal now is to use this content type in a library. We can also set it as Default while we’re at it…

  • I have created a library in SPO called “My Perso Library
  • Allow management of content types is set to the default (No) – No need to enable it prior to running the cmdlet

 

$MyCT = "MyNewCT"
Add-PnPContentTypeToList -List "My Perso Library" -ContentType $MyCT -DefaultContentType
NewCTasDefault

New content type set as Default

SetAsDefault

Set as Default in Library settings

 

Loading Likes...

Leave a Reply

Enjoying this blog? Spread the word to help others :-)

%d bloggers like this: