SharePoint Online: Change Column Order in Content Type using PowerShell

Requirement: Change Column Order in SharePoint Online Content Type using PowerShell

How to Change Column Order in SharePoint Online Content Type?
Column order in SharePoint Online content types defines the order of fields displayed in list forms such as New, Edit and View pages. To change the order in which the columns associated with a content type are displayed on list pages, do the following:
  • Navigate to the SharePoint Online site where the content type was created.
  • Click on Settings gear >> Select Site Settings
  • On the Site Settings page, select the "Site Content Types" link under "Web Designer Galleries" section.
  • On the Site Content Types page, click on the name of the content type to be edited >> Click the "Column Order" link  from the Columns section.
  • On the Column Order page, You can change the position from top numbers to set the sort order for the columns.
    sharepoint online change column order in content type powershell
  • Once done, Set whether the column ordering changes to be pushed to all content types based on this type.
  • Click the OK button to save the changes.
This sets the column order for a list content type at list level. Column order can be set for both list content types and site content types.

SharePoint Online: PowerShell to Set Content Type Column Order
#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 = "Contacts"
$ContentTypeName = "Announcement"
$ColumnOrder = @("Title", "Category", "Body", "Expires")

#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 Type
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List.ContentTypes)
    $Ctx.ExecuteQuery()

    #Get the Content Type from List
    $CType = $List.ContentTypes | Where {$_.Name -eq $ContentTypeName}
    If($CType -ne $Null)
    {
        $Ctx.Load($CType.FieldLinks)
        $Ctx.ExecuteQuery()

        #Set Content type field Order
        $CType.FieldLinks.Reorder($ColumnOrder)
        $CType.Update($False)
        $Ctx.ExecuteQuery()
        Write-host -f Green "Content Type Order has been updated Successfully!"
    }
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
} 

Change Column Order in Content Type using PnP PowerShell
This time, lets change the column order in Site content type using PnP PowerShell
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/projects"
$ContentTypeName ="Crescent Project Proposal V2"
$FieldOrder = @("Project_x0020_Name","Project_x0020_Description","Project_x0020_Start_x0020_Date","Project_x0020_Status","Project_x0020_Manager")

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

#Get the content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName

#Update column Order in content type
$FieldLinks =  Get-PnPProperty -ClientObject $ContentType -Property "FieldLinks"
$FieldLinks.Reorder($FieldOrder)
$ContentType.Update($True)
Invoke-PnPQuery
SharePoint Online: Change Column Order in Content Type using PowerShell SharePoint Online: Change Column Order in Content Type using PowerShell Reviewed by Salaudeen Rajack on July 11, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.