SharePoint Online: Set Default Content Type using PowerShell

Requirement: Set Default Content Type in SharePoint Online List using PowerShell.

How to change the default content type in SharePoint Online?

A SharePoint list can have multiple content types. There are situations where you may want to set the default content type in the new button. You can specify which of the available ones is the default for the list or library by setting the order of the listed content types. To set a default content type, do the following:

  1. Navigate to the SharePoint Online list or library where content types need to be managed.
  2. Go to List or Library Settings >> On the Settings page, click on the “Change New Button Order and Default Content Type” link under the Content Types section.
  3. On the Change New Button Order page, You can move the relevant content type to the first position. The content type which appears in the first position will become the default content type for the list.
  4. Click the OK button to save the changes.

You can update the content type ordering and visibility details for the list or library.

Set Default Content Type in SharePoint Online list using PowerShell

How to make a content type default in SharePoint Online? Well, Here is the PowerShell script to reorder content types in a SharePoint Online List. The Item in the first index becomes the default content type automatically. This script doesn’t remove or re-add the content type from the list. It simply changes the order of the content type sequence.

#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"
    
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Business Contacts"
$ContentTypeName = "Contacts"

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the List Content Types
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List.ContentTypes)
    $Ctx.ExecuteQuery()

    #Create a List for content types
    $ContentTypeOrder = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]

    #Add the Default content type to the First Index  
    ForEach($ContentType in $List.ContentTypes)
    {
        If($ContentType.Name -eq $ContentTypeName)
        {
            $ContentTypeOrder.Add($ContentType.Id)
        }
    }
    #Add Rest of the content types to the Content type Order List
    ForEach($ContentType in $List.ContentTypes)
    {
        If($ContentType.Name -ne $ContentTypeName -and $ContentType.Name -ne "Folder")
        {
            $ContentTypeOrder.Add($ContentType.Id)
        }
    }
    
    #Set Content type Order
    $List.RootFolder.UniqueContentTypeOrder = $ContentTypeOrder
    $List.RootFolder.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Default Content Type has been updated successfully!"
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

Set Default Content Type in SharePoint Online List using PnP PowerShell

To change the default content type in the SharePoint Online list or library, use this PowerShell:

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Projects"
$ContentTypeName = "Crescent Projects V2"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Set Default Content Type of the List
Set-PnPDefaultContentTypeToList -List $ListName -ContentType $ContentTypeName

Please note, the content type must be added to the list already!

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Online: Set Default Content Type using PowerShell

  • Hello Salaudeen,

    Thank you for this information as well.

    Please I am more particular about how to do this for multiple libraries, especially if it can fetch from an excel sheet to avoid errors.
    For instance, I wrongly typed a library name, and the code to add content type to the library failed, cos it was manually entered.

    Please really assist with multiple libraries way out.

    Thanks.

    Reply
  • How to add a retention like a 1 year, 2 year etc to a content type for a site collection I have created. I need to be able to do this using powershell PNP. Is there a way to do this?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *