5

I have some items created using New-Item but they are automatically sorted alphabetically. I would much prefer these items to be sorted by Created date.

How do you change the sort order of items using SPE?

7

There are two places I can think of when it comes to changing the sort order.

Item level sort order

Select an item and change the view in the ribbon to include Standard fields. You should see something like this:

Sortorder

These values can be changed from the ribbon or programmatically and the value is an integer (e.g. 100, 200).

Example: The following changes an item's sort order. Assumes you know the precise order you want to achieve.

$item = Get-Item -Path "master:" -ID "{C9CB2B73-04B3-43F8-B36A-D64C77E35F79}"
$item.Editing.BeginEdit()
$item.Fields["__Sortorder"].Value = 200
$item.Editing.EndEdit() > $null

Subitem level sort order

This is likely the option you want to go with. You don't have to worry about changing the sort manually every time, and it goes beyond a simple numerical value.

Subitem sorting context menu

Sort order menu

Example: The following sets the sort order on the parent node which results in this behavior cascading to the immediate children.

function Set-SortOrder {
    [CmdletBinding(DefaultParameterSetName="SortOrder")]
    param(
        [Parameter()]
        [ValidateNotNull()]
        [item]$Item,

        [Parameter(ParameterSetName="SortOrder")]
        [ValidateSet("Created","Default","Display name","Logical","Reverse","Updated")]
        [string]$SortOrder,

        [Parameter(ParameterSetName="Reset")]
        [switch]$Reset
    )

    $subitemsSortOrderFieldId = "{6FD695E7-7F6D-4CA5-8B49-A829E5950AE9}"

    $sortValue = & { 
        switch($SortOrder) {
            "Created" {
                "{C1FF011E-B02A-44E3-8444-9FC89CFC28CE}"
            }
            "Default" {
                "{781247D2-9785-400F-8935-C818EC757967}"
            }
            "Display name" {
                "{44D1A0D2-E17B-4DAA-ADDF-53F2E8F58525}"
            }
            "Logical" {
                "{EA1DECB2-B4F2-4AE0-99A8-30FDED9B8B50}"
            }
            "Reverse" {
                "{C3E3F0E3-0162-4F1F-AB3E-40348E371A3F}"
            }
            "Updated" {
                "{32416A95-4197-4D33-8CE7-7BB4FFEBEB42}"
            }
        }
    }

    $item.Editing.BeginEdit()
    if($Reset.IsPresent) {
        $item.Fields[$subitemsSortOrderFieldId].Reset()
    } else {
        $item.Fields[$subitemsSortOrderFieldId].Value = $sortValue
    }
    $item.Editing.EndEdit() > $null
}

$item = Get-item -Path "master:" -ID "{A1D0B3BC-CCDA-41FF-B59A-33AB84BF950B}"
# Reset the subitem sorting to Standard values
Set-SortOrder -Item $item -Reset
# Change the subitem sorting to based on Created Date
Set-SortOrder -Item $item -SortOrder Created

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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