Sort a list block by a field whose name is specified in a URL querystring parameter

Here’s some example code that pulls a sort field from the URL querystring and substitutes it into the request for the list data, so that the net effect is that you can load mysite.com/listpage?sortingField=Email and the list block on that page will sort by the Email column instead of whatever was originally specified in the list block.

<script>
    const urlParams = new URLSearchParams(location.search);
    const sortingField = urlParams.get('sortingField');
    if (sortingField) {
        (function(send) {
              XMLHttpRequest.prototype.send = function() {
                if (typeof arguments[0] === "string") {
                    var b = JSON.parse(arguments[0]);
                    b.sortingOption.sortingField = sortingField;
                    const body = JSON.stringify(b);
                    arguments[0] = body;
                }
                send.apply(this, arguments);
              };
            })(XMLHttpRequest.prototype.send);
    }
</script>

Note: the data will not display if the value of the sortingField querystring parameter is not a valid field name.

4 Likes

I also figured out how to make the URL query string track changes to list search and filters. Please reply if you want that code.

1 Like

I would like to see that code! :slight_smile: