SharePoint Online: Add Content Type to List or Library using PowerShell

Requirement: SharePoint Online PowerShell to add a content type to the library.

How to add 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 “Add from existing site content types” Link 
    sharepoint online powershell add content type to library
  • From the Add content types page, select your content type to add and click on the “Add” button and then “OK” once.
    sharepoint online add content type to list
  • 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!

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

6 thoughts on “SharePoint Online: Add Content Type to List or Library using PowerShell

  • 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.

    Reply
  • 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.

    Reply
  • 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?

    Reply
    • 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?

      Reply
  • 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

    Reply

Leave a Reply