2

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?

6

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.

  • That is a great help and solves my problem!! Cheers. I have written a CSOM PowerShell function as well and I have posted as an answer, if this is helpful. – Asad Refai May 29 '15 at 10:28
0

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!

  • 1
    And how do yo remove and add Content Types via PowerShell? This is what OP is asking. Please update your answer. Thank You – Benny Skogberg Jul 1 '15 at 7:19
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.