In a Custom List there are more than 1 content types e.g. "Content Type 1", "Content Type 2"
including OOB Item
.
Using PowerShell how can they be reordered?
Here you go:
Add-PSSnapin Microsoft.SharePoint.Powershell
$url = "http://sharepoint/sites/site1"
$order = "Content Type 1", "Content Type 2", "Item"
$web = Get-SPWeb $url
$list = $web.ListS["Your Custom List"]
if($list.ContentTypes[$order[0]] -ne $null)
{
$currentListOrder = $list.RootFolder.ContentTypeOrder
for($i = $currentListOrder.Count; $i -gt 0; $i--)
{
$lct = $currentListOrder[$i]
$disableoutput = $currentListOrder.Remove($lct)
}
$currentListOrder.Add($list.ContentTypes[$order[0]])
$disableoutput = $currentListOrder.Remove($currentListOrder[0])
foreach($ctToAdd in $order | where { $_ -ne $order[0]})
{
$currentListOrder.Add($list.ContentTypes[$ctToAdd])
}
$list.RootFolder.UniqueContentTypeOrder = $currentListOrder
$list.RootFolder.Update()
}
This is tested and working fine. If you need any explanation of the script I am happy to add comments.
The PowerShell function using Client Object Model
. We don't need to be on the SharePoint server to run the below function. Just need client components installed on our machine
Link to download SharePoint Server 2013 Client Components SDK
Below function can also referred on GitHub and This Blog
#Load SharePoint client assemblies
#
Try{
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}
catch {
Throw "Unable to load SharePoint Client runtime"
}
function Reorder-ContentTypesInList()
{
param(
[Parameter(Mandatory=$true)][string]$url,
[Parameter(Mandatory=$true)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$true)][string]$listName,
[Parameter(Mandatory=$true)][string[]]$ContentTypeNamesInOrder
)
begin{
try
{
#get Client Object
#
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$context.Credentials = $credentials
#Retrieve List
#
$list = $context.Web.Lists.GetByTitle($listName)
$context.Load($list)
$context.ExecuteQuery()
#Get Content Types from a list
#
$contentTypes = $list.ContentTypes
$context.Load($contentTypes)
$context.ExecuteQuery()
}
catch{
Write-Host "Error while getting context. Error -->> " + $_.Exception.Message -ForegroundColor Red
}
}
process{
try
{
#Making generic list of content type ids in passed order
#
$ctList = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]
Foreach($ct in $ContentTypeNamesInOrder){
$ctToInclude = $contentTypes | Where {$_.Name -eq $ct}
$ctList.Add($ctToInclude.Id)
}
#Updating content type order
#
$list.RootFolder.UniqueContentTypeOrder = $ctList
$list.Update()
$context.Load($list)
$context.ExecuteQuery()
Write-Host "Content Types Reordered successfully" -ForegroundColor Green
}
catch
{
Write-Host ("Error while reordering content types in a list. Error -->> " + $_.Exception.Message) -ForegroundColor Red
}
}
end{
$context.Dispose()
}
}
$credentials = Get-Credential
$ContentTypesInOrder = "Content Type 1", "Content Type 2", "Content Type 3"
Reorder-ContentTypesInList 'http://YourSite' $credentials 'Your List Name' $ContentTypesInOrder
only by removing and re-adding...
had the same problem... googled whole 3 days for sorting content types... google page 20 told me, it's only possible by removing and re-adding!