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.

Advertisements

About pgbhoyar

Prashant G Bhoyar is a Microsoft Office Server & Services MVP and Microsoft Certified Professional. He currently works as a Senior Consultant at WithumSmith+Brown, PC. He is a trusted adviser and Subject Matter Expert and specializes in the development and post-implementation adoption of complex custom solutions in Office 365 and SharePoint. Prashant has supported many government agencies and non-profit organizations in the Washington D.C. metropolitan area. Prashant was the technical reviewer of the book "Pro SharePoint 2013 Administration." He serves on the leadership committee for the DC Metro Office 365 User Group and SharePoint Saturday Baltimore event and SharePoint Saturday Washington DC event. He actively speaks at technical conferences across the country, most recently in New Orleans, Chicago, Houston, Seattle, Baltimore, Philadelphia, as well as, at many events throughout North America. 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 Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s