SharePoint Online: Set Default Content Type using PowerShell

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

How to Set Default Content Type in SharePoint Online List or Library?
A SharePoint list can have multiple content types. There are situations where you may want to set the default content type which appear in 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 "Change New Button Order and Default Content Type" link under Content Types section.
  3. On the Change New Button Order page, You can move the relevant content type to the first position. Content type which appears at first position will become the default content type for the list.
  4. Click the OK button to save the changes.
You can update to the content type ordering and visibility details for the list or library.


Set Default Content Type in SharePoint Online list using PowerShell
Here is the PowerShell script to reorder content types in a SharePoint Online List. 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 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
#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 contnet type must be added to the list already!
SharePoint Online: Set Default Content Type using PowerShell SharePoint Online: Set Default Content Type using PowerShell Reviewed by Salaudeen Rajack on July 10, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.