Feeds:
Posts
Comments

Archive for March, 2017

As SharePoint Developers, we are often challenged with requests to sort list item data seemingly arbitrarily. In these cases, none of the field data can be used to sort ascending or descending and get the results we are seeking. Often, we end up creating a Number column and setting a value so we can sort on that value. The trouble here is that you must manually update all of the other items. This can be painful if you’re trying to move an item with Order = 50 to Order = 1: You must change the Order value on 49 other items!

Links Lists have a handy link in the Items menu labelled “Change Item Order.” The page this link takes you to allows you to change the order of items in the Links List just like you would change the order of fields in a list view. SharePoint then stores the numerical sort order in a hidden field on the list which the Links Lists view uses with an orderBy.

So, excellent, there is something out of box to do what we want to do. Hooray! Now, how do we make use of it? Turns out, if you add a column to your custom list that is identical to the hidden column used in the Links List, you can take advantage of the “Change Item Order” page for your custom list! Here’s how I did it:

Using SharePoint REST API, I discovered the SchemaXml property for the hidden Order field in the Links List is:

<Field ID=\"{ca4addac-796f-4b23-b093-d2a3f65c0774}\" ColName=\"tp_ItemOrder\" RowOrdinal=\"0\" Name=\"Order\" DisplayName=\"Order\" Type=\"Number\" Hidden=\"TRUE\" SourceID=\"http://schemas.microsoft.com/sharepoint/v3\" StaticName=\"Order\" FromBaseType=\"TRUE\" />

I then simply generated a POST using the REST API once again to create a field on my custom list using the SchemaXml above. You can, of course, use whatever app you wish to do the POST. My favorite is Postman. Here’s how I setup the request to send:

  1. POST to [your site URL]/_api/web/lists/getbytitle(‘[title of your custom list]‘)/fields/createfieldasxml
  2. Headers:
    1. Accept: application/json
    2. Content-Type: application/json
    3. X-RequestDigest: [a current request digest]
  3. Body:
    {
        "parameters": {
           "__metadata": {
              "type": "SP.XmlSchemaFieldCreationInformation"
           },
           "SchemaXml": "&lt;Field ID=\"{ca4addac-796f-4b23-b093-d2a3f65c0774}\" ColName=\"tp_ItemOrder\" RowOrdinal=\"0\" Name=\"Order\" DisplayName=\"Order\" Type=\"Number\" Hidden=\"TRUE\" SourceID=\"http://schemas.microsoft.com/sharepoint/v3\" StaticName=\"Order\" FromBaseType=\"TRUE\" /&gt;"
        }
    }

This adds a hidden Order field to your custom list. The only thing left to do is access the “Change Item Order” page for your list and start setting item order. Here’s the URL:

[your site URL]/_layouts/15/Reorder.aspx?List=[your list GUID]

If you want to get the ordering data for your client-side app, here’s an important tip: The SharePoint REST API does not return the Order field when you simply execute a call to _api/web/Lists…/items. If you add Order to a $select option—like _api/web/Lists…/items?$select=Order—the item ordering set by the “Change Item Order” page will be returned.

Happy ordering! Let me know below if you find any issues doing this.

Update/Note, 11/7/2017: The “Change Item Order” page only supports up to 100 items at a time. It pages items in groups of 100. Thus, it gets a little tricky to move an item around when you’re dealing with hundreds of items or more.

Advertisements

Read Full Post »