How to ReOrder SharePoint List Items using Custom Code/PowerShell and SharePoint Designer in SharePoint 2010 and SharePoint 2013

The functionality to “Reorder list items” is inbuilt for “Links” lists both in SharePoint 2010 and SharePoint 2013.

051513_0103_HowtoReOrde1.png

   “Change Item Order” option in SharePoint 2013 Links List.

051513_0103_HowtoReOrde2.png

 “Change Item Order” option in SharePoint 2010 Links List.

 How does this “Change Item Order” button work?

SharePoint uses hidden column “order” to set the order. When we select the “Change Item Order” option, SharePoint uses “/_Layouts/Reorder.aspx?List=ListGUID” page to reorder the list items.

But this wonderful feature is hidden for other list templates and only available with OOTB Links Lists. I wonder why?

 In this blog post I have listed different methods we can use to enable this feature for other lists.

 Method 1: Use Console Application/PowerShell script. This method is preferable because we can reuse it for different lists with ease. We just need to provide the site url and the list name.

 C# Console Application You can download this file from here.

using Microsoft.SharePoint;
namespace ReOrderSharePointListItems
{
  class ReOrderSharePointListItems
  {
    static void Main(string[] args)
    {
      using (var site = new SPSite("yourSiteCollectionUrl"))
      {
        using (var web = site.OpenWeb())
        {
          var list = web.Lists["yourListTitle"];
          SetButton(list, web);
          SetOrder(list, web);
        }
      }
    }

   /// <summary>
   /// Set Button "Change Item Order" in
   /// Top Ribbon of List
   /// </summary>
   /// <param name="list"></param>
   /// <param name="web"></param>
  static void SetButton(SPList list, SPWeb web)
  {
    list.UserCustomActions.Clear();
    SPUserCustomAction action = list.UserCustomActions.Add();
    action.Location = "CommandUI.Ribbon";
    action.Sequence = 85;
    action.Title = "PS.OrderItems";
    action.Rights |= SPBasePermissions.EditListItems;
            action.CommandUIExtension =
    string.Format(
    @"<CommandUIExtension>
    <CommandUIDefinitions>
    <CommandUIDefinition
            Location=""Ribbon.ListItem.Actions.Controls._children"">
    <Button Id=""ReOrderAction.Button""
            TemplateAlias=""o1"" Command=""ReOrderCommand""
            CommandType=""General"" LabelText=""Change Item Order""
    Image16by16=""/_layouts/1033/images/formatmap16x16.png""
            Image16by16Top=""-192"" Image16by16Left=""-144""
    Image32by32=""/_layouts/1033/images/formatmap32x32.png""
            Image32by32Top=""-192"" Image32by32Left=""-288""/>
    </CommandUIDefinition>
    </CommandUIDefinitions>
            <CommandUIHandlers>
    <CommandUIHandler Command =""ReOrderCommand""
            CommandAction=""{0}/_layouts/reorder.aspx?List={1}"" />
    </CommandUIHandlers>
            </CommandUIExtension>",
    web.ServerRelativeUrl.TrimEnd('/'), list.ID);
    action.Update();
  }

  /// <summary>
  /// Order Default view by field "order"
  /// </summary>
  /// <param name="list"></param>
  /// <param name="web"></param>
  static void SetOrder(SPList list, SPWeb web)
  {
    SPView view = list.DefaultView;
    view.Query = @"<OrderBy><FieldRef Name=""Order""
                         Ascending=""TRUE""/></OrderBy>";
    view.Update();
  }
 }
}

PowerShell Script: You can download this script from here.

[void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | out-null
# Add "Change item Order" button at target List
$siteUrl = "yourSiteCollectionUrl";
$listTitle = "yourListTitle";
$site = Get-SPSite -Identity $siteUrl;
$web = $site.OpenWeb();
$list = $web.Lists[$listTitle];
$list.UserCustomActions.Clear();
$action = $list.UserCustomActions.Add();
$action.Location = "CommandUI.Ribbon";
$action.Sequence = 85;
$action.Title = "PS.OrderItems";
$action.CommandUIExtension =
[string]::Format(
"<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
  Location=""Ribbon.ListItem.Actions.Controls._children"">
<Button Id=""ReOrderAction.Button"" TemplateAlias=""o1""
  Command=""ReOrderCommand"" CommandType=""General""
  LabelText=""Change Item Order""
Image16by16=""/_layouts/1033/images/formatmap16x16.png""
  Image16by16Top=""-192"" Image16by16Left=""-144""
Image32by32=""/_layouts/1033/images/formatmap32x32.png""
  Image32by32Top=""-192"" Image32by32Left=""-288""/>
</CommandUIDefinition>
</CommandUIDefinitions>
  <CommandUIHandlers>
<CommandUIHandler Command =""ReOrderCommand""
  CommandAction=""{0}/_layouts/reorder.aspx?List={1}"" />
</CommandUIHandlers>
 </CommandUIExtension>",
$web.ServerRelativeUrl.TrimEnd('/'), $list.ID);
$action.Update();
$view = $list.DefaultView;
$view.Query = [string]::Format("<OrderBy><FieldRef Name=""Order""
Ascending=""TRUE""/></OrderBy>");
$view.Update();
Write-Host "-------------------------------------"
Write-Host "Change item order button added successfully for
the list :  " $list.Title -foregroundcolor Green -nonewline
Write-Host " in the site  : " $site.Url -foregroundcolor Green
Write-Host "-------------------------------------"
$web.Dispose();
$site.Dispose();

Method 2: Use SharePoint designer and PowerShell.

1. Since “order” column is hidden, we need to use following PowerShell script to make this field visible. Once this field is visible, we can use it in List Views. You can download this script from here.

# Change List Schema to unhide order field.
$siteUrl = "yourSiteUrl;
$listTitle = "yourListTitle";
$site = Get-SPSite -Identity $siteUrl;
$web = $site.OpenWeb();
$list = $web.Lists[$listTitle];
$field=$list.Fields["Order"];
$field.SchemaXml=
$field.SchemaXml.Replace("Hidden=""TRUE""","Hidden=""FALSE""");
Write-Host "-------------------------------------"
Write-Host "Order field is set to visible in
list :  " $list.Title -foregroundcolor Green -nonewline
Write-Host " in the site  : " $site.Url -foregroundcolor Green
Write-Host "-------------------------------------"
$web.Dispose();
$site.Dispose();

2. Open the list in SharePoint Designer

3. Click on “Custom Actions” and then select “View Ribbon”

051513_0103_HowtoReOrde3.png

4. Enter following information.

  • Name : “Change Item Order”
  • Navigate to URL:  {SiteUrl}/_layouts/Reorder.aspx?List={ListGUID}
  • Button Image URL (16×16): {SiteUrl}/_layouts/1033/images/formatmap16x16.png
  • Button Image URL (32×32): {SiteUrl}/_layouts/1033/images/formatmap32x32.png

051513_0103_HowtoReOrde4.png

5. Click OK. This will add the custom action in the list ribbon as shown below.

051513_0103_HowtoReOrde5.png

 

6. Next step is to modify the default view of the list and sort the items by field “Order”.

051513_0103_HowtoReOrde6.png

7. All set.

Note : “Change Item Order” option does not modify the version history of list items. So if you are using Content Deployment, reordering the list items will not move changes to target environment.

About pgbhoyar

Prashant G Bhoyar is a Microsoft AI MVP and Microsoft Certified Professional. He currently works as a Senior Solution Architect at WithumSmith+Brown, P.C. (formerly Portal Solutions), one of the top 30 advisory and accounting firms in the United States. He is a trusted advisor and Subject Matter Expert in Intelligent Business Process Automation, development and post-implementation adoption of complex custom solutions in AI, Machine Learning, Bots, Azure, Office 365, and SharePoint. Prashant has supported a multitude of government agencies and non-profit organizations in the Washington D.C. metropolitan area. Prashant is the co-author of the book “PowerShell for Office 365 “and is the technical reviewer of the book "Pro SharePoint 2013 Administration." He serves on the leadership committee for the Azure Data Fest conference, Artificial Intelligence Fest conference, Artificial Intelligence and Machine Learning User Group, DC Metro Office 365 User Group, SharePoint Saturday Baltimore event, and SharePoint Saturday Washington DC event. He is a renowned international speaker and actively speaks at technical conferences, most recently in India, Canada, England, Bangladesh, Peru , Washington DC, Austin, New York, Chicago, Seattle, New Orleans, Baltimore, Philadelphia. Fun fact??, Prashant is a recipient of the "Antarctic Service Medal of the United States of America" for his outstanding service in Antarctica.
This entry was posted in SharePoint 2013, SharePoint 2013 Development and tagged , , , , , , , , . Bookmark the permalink.

5 Responses to How to ReOrder SharePoint List Items using Custom Code/PowerShell and SharePoint Designer in SharePoint 2010 and SharePoint 2013

  1. service says:

    Quality content is the crucial to attract the visitors to go to see the website, that’s what this website is providing.

    Like

  2. Weird things when I change the items order it assigns all the changed links order = 350.

    Like

  3. Weird thing, when I change an items order the order number becomes 350 for all the items that I change the position.

    For example

    link a is 100.
    link b is 200
    link c is 300
    link d is 400

    if I change the order of the links all order number will become 350.

    Like

  4. Pingback: How to ReOrder SharePoint List Items | My Blog

Leave a comment