SharePoint Online: Add Content Type to List or Document Library using PowerShell
Requirement: SharePoint Online PowerShell to add a content type to the library.
How to add a content type to a list or library in SharePoint Online?
In SharePoint Online, you can add content types to a list to help define the type of information that is stored in the list. For example, you might add a content type for “Product” to a list that stores product information. This would allow you to create new items in the list that are automatically associated with the “Product” content type. This article will show you how to add a content type to a list in SharePoint Online.
Assuming you have the content type already created, here are the steps to add a content type to the list in SharePoint Online:
Step 1: Enable content types management for the list, if it’s not enabled already
Before adding a content type to a list or library, you must enable the list or library to support content types.
- Navigate to the List settings >> Click on the “Advanced Settings” link under General Settings.
- Set “Yes” for the “Allow management of content types?” option and hit the OK button at the bottom of the page to save your changes.
Step 2: Add Content Type to List or Library
Once content types are enabled, here is how you can add a content type to a list or library in SharePoint Online:
- Go to the list settings page again, You’ll find the “Content Types” group created with the default content type listed (E.g. You’ll find the “Item” content type listed for custom lists).
- Click on the “Add from existing site content types” link.
- From the Add content types page, select your content type to add and click on the “Add” button and then “OK” once.
- That’s all. Now you’ll find the content type you added under the “Content Types” group of list settings.
Alright. Now, Let’s see how to add a content type to a list in SharePoint Online using PowerShell.
SharePoint Online PowerShell to Add Content Type to Library
Let’s add a content type to the list in SharePoint Online using PowerShell:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Add-ContentTypeToList()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $CTypeName
)
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List
$List = $Ctx.web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Enable managemnt of content type in list - if its not enabled already
If($List.ContentTypesEnabled -ne $True)
{
$List.ContentTypesEnabled = $True
$List.Update()
$Ctx.ExecuteQuery()
Write-host "Content Types Enabled in the List!" -f Yellow
}
#Get all existing content types of the list
$ListContentTypes = $List.ContentTypes
$Ctx.Load($ListContentTypes)
#Get the content type to Add to list
$ContentTypeColl = $Ctx.Web.ContentTypes
$Ctx.Load($ContentTypeColl)
$Ctx.ExecuteQuery()
#Check if the content type exists in the site
$CTypeToAdd = $ContentTypeColl | Where {$_.Name -eq $CTypeName}
If($CTypeToAdd -eq $Null)
{
Write-host "Content Type '$CTypeName' doesn't exists in '$SiteURL'" -f Yellow
Return
}
#Check if content type added to the list already
$ListContentType = $ListContentTypes | Where {$_.Name -eq $CTypeName}
If($ListContentType -ne $Null)
{
Write-host "Content type '$CTypeName' already exists in the List!" -ForegroundColor Yellow
}
else
{
#Add content Type to the list or library
$AddedCtype = $List.ContentTypes.AddExistingContentType($CTypeToAdd)
$Ctx.ExecuteQuery()
Write-host "Content Type '$CTypeName' Added to '$ListName' Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding Content Type to the List!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Project Frosty"
$CTypeName="Project Template"
#Call the function
Add-ContentTypeToList -SiteURL $SiteURL -ListName $ListName -CTypeName $CTypeName
SharePoint Online PnP PowerShell to Add Content Type to Library
Here is how to add a content type to the document library in SharePoint Online using Add-PnPContentTypeToList PnP PowerShell:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ContentTypeName="Crescent Invoice Template V2"
$ListName="Invoices"
#Get Credentials to connect
$Cred = Get-Credential
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get the content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName
If($ContentType)
{
#Add Content Type to Library
Add-PnPContentTypeToList -List $ListName -ContentType $ContentType
}
else
{
Write-host -f Yellow "Could Not Find Content Type:"$ContentTypeName
}
Use -DefaultContentType switch to make the content type as default content type! How about adding some error handling to the code, such as: Checking if the given list exists, checking if the content type is not added to the list already, etc.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Purchase"
$ListName = "Site Pages"
$ContentTypeName = "Site Page"
Try {
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Get the context
$Ctx = Get-PnPContext
#Check if the List or Library Exists
$List = Get-PnPList -Identity $ListName
If($List -ne $Null)
{
#Add Content type to the list
$ContentType = Get-PnPContentType -Identity $ContentTypeName
If($ContentType)
{
#Check if the List has the content type already
$ListContentType = Get-PnPContentType -List $ListName -Identity $ContentTypeName -ErrorAction SilentlyContinue
If(!$ListContentType)
{
#Add Content Type to List
Add-PnPContentTypeToList -List $ListName -ContentType $ContentType
Write-host -f Green "'$ContentTypeName' content type added to the List '$ListName'"
}
Else
{
Write-host -f yellow "'$ContentTypeName' content type already exists in the List!"
}
}
Else
{
Write-host -f Yellow "Could Not Find the '$ContentTypeName' Content Type!"
}
}
Else
{
Write-Host -f Yellow "List '$ListName' does not exist!"
}
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
I am running into an issue where I have added a new Content Type to the Content Type Hub and now am wanting to add this to a list via powershell but I get the error “Could Not Find Content Type: *Name of content type*”.
Is there a limitation where if the content type isn’t from the same source as the site the script doesn’t work
Yes! You have to run this once: Add-PnPContentTypesFromContentTypeHub -ContentTypes “Your-ContentType-ID” -Site “Your-Site-URL”
The script works fine for me but when I create a new site the content type is not synced and not used in the site yet. Now I saw your reply but the following command is not available/recognized Add-PnPContentTypesFromContentTypeHub.
ps: I’m not a developer and just starting to learn PowerShell
Ensure you have the latest PnP PowerShell module installed on your machine! How to Install PnP PowerShell Module for SharePoint Online?
Hello Salaudeen,
I sincerely appreciate your effort to help out, but I am a novice to PowerShell. I am really confused at this state as to how to go about the instructions given.
Please I seek that you really help with a comprehensive way to go about the adding of content type to multiple document libraries.
Thanks in anticipation to your true response.
Hello Salaudeen, Thank you for the response, it worked. But I still have concerns, which i seek you help out with.
1) It prompts to sign in at every single library update, is there a way to go about/avoid it?
2) I have about 400libraries to map the content type with and on different sites, is there a way to fetch the libraries name from an excel sheet, without entering them manually? Just the way you helped with creating multiple document libraries from an excel sheet to different sites.
3) With Respect to (1) above, any mistake in a sign in, skips mapping of the content type to that particular library.
4) Please help also with how to make the newly added content type the default for bulk update.
1. You can saved passwords in the script and avoid password prompts. Or move the Get-Credential out of the function. Reference: How to Connect to SharePoint Online using CSOM PowerShell?
If you are using PnP PowerShell, Use “-Interactive” switch for “Connect-PnPOnline” cmdlet instead of “-Credentials $Cred ”
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
2. You can read the CSV and process it in your PowerShell scripts. Use the “Search” in this site, You’ll find a lot of such script references.
3. Place your script inside “Try-Catch” to handle the errors.
4. Yes, It’s possible to set the default content type with PowerShell as: How to Set the Default Content Type in SharePoint Online using PowerShell?
Hello Salaudeen,
Please assist with if I have multiple libraries that I wish to add content type to.
How do I go about that please?
If you know the library names, Just have your Library names in an Array and call the function with each library name! E.g.
$Libraries = @(“Shared Documents”,”Branding”, “Team Docs”, “Products”)
#Call the function to add content type to each document library
$Libraries | ForEach-Object { Add-ContentTypeToList -SiteURL $SiteURL -ListName $_ -CTypeName $CTypeName }
In case, You want to add to all available document libraries, use: How to Get All Document Libraries in a SharePoint Online site using PowerShell?
I am getting this error quiet recently, what could that be. I have successfully used the script before. Get-PnPContentType : Exception has been thrown by the target of an invocation. Thanks