Share via

Ordering items using Script Editor

sco gordo 301 Reputation points
2021-06-07T14:32:35.53+00:00

Hi, we have some old custom code that's sorting items based on "ID" rather than "Display Order". It's NOT done through the CQWP or the View.
Quickest way I can think of rectifying is by using Script Editor on the page with some orderby code. Any sample code? XLST, JavaScript,...?
Thanks!

Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint Server | Development
0 comments No comments

4 answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,136 Reputation points
    2021-06-08T06:42:43.407+00:00

    Hi @sco gordo ,

    You could use camel query in JSOM code to sort the list item by the column Display order, Below is my demo for you: 103255-orderitems.txt

    var clientContext = new SP.ClientContext();  
    var oList  = clientContext.get_web().get_lists().getByTitle('yourList');  
    var query = new SP.CamlQuery();  
    query.set_viewXml('<View><Query><OrderBy><FieldRef Name="Display_x0020_Order" /></OrderBy></Query></View>');  
    this.oListItems  = oList.getItems(query);  
    clientContext.load(oListItems);  
    clientContext.executeQueryAsync(onsuccess, onfailed);  
    

    103289-image.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. sadomovalex 3,636 Reputation points
    2021-06-07T14:58:15.37+00:00

    didn't see your old custom code (is it written on C# (server OM or CSOM?) or javascript?) but in general it is possible to get list items and display them in needed order using javascript object model, e.g. Using Javascript to get list items, can I change item order.

    1 person found this answer helpful.
    0 comments No comments

  3. lindalu-MSFT 161 Reputation points Microsoft Employee Moderator
    2021-06-11T17:47:49.78+00:00

    office-scripts-dev tag is specifically for Office Scripts in Excel.

    0 comments No comments

  4. sco gordo 301 Reputation points
    2021-06-07T16:28:47.623+00:00

    Thanks sadomovalex. I don't have access to the code at the moment. Rather than modify it, I'm hoping to supersede it with the on-page Script Editor, at least short-term.

    More info:

    I have list items, with metadata for Title, ID, and Display Order. The Display Order is obviously intended for presentation, but for whatever reason is not functioning. ID is associated with a details page and is currently determining the order on page as well.

    • item1, ID=7, Display order =3
    • item1, ID=8, Display order =2
    • item1, ID=9, Display order =1

    Basically, the items are being displayed like:

    <a href="Details.aspx?PID=7"><h2>item1</h2></a>

    <a href="Details.aspx?PID=8"><h2>item2</h2></a>

    <a href="Details.aspx?PID=9"><h2>item3</h2></a>

    when I need the displayed like

    <a href="Details.aspx?PID=9"><h2>item3</h2></a>

    <a href="Details.aspx?PID=8"><h2>item2</h2></a>

    <a href="Details.aspx?PID=7"><h2>item1</h2></a>

    using the Display Order rather than ID.

    I'm hoping there's some incredibly simple bit of code I can add ;)

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.