1

I am getting the fields of a list view using the javascript object model

view.get_viewFields()

However, this does not return the fields in order which they are configured in the view. It just returns the schema xml with the internal names and we can read the field names using the "get_item()" method. I have observed that the order that is returned is the order in which the columns were created in the list.

Is there a way to read the current order of fields in list view using javascript object model ?

4
  • what are you trying to achieve with this information? maybe there is an other way to do what you want to do.
    – Gwny
    Commented Jun 19, 2015 at 6:50
  • i have written a sharepoint hosted app where user selects list items and clicks custom ribbon action app. SP hosted app opens in modal dialog and displays the records in table which then can be printed. If the order of the columns is changed in the view, app does not present columns in that order. Commented Jun 19, 2015 at 7:14
  • for me, the get_viewFields() method returns the order in which the fields appear in the view, not the order they were created as you said.
    – MdMazzotti
    Commented Jun 19, 2015 at 7:57
  • did you change the order of the fields in the view after creating the columns ? Commented Jun 19, 2015 at 8:20

1 Answer 1

0

You can give it a try using REST query as below:

<script src='http://code.jquery.com/jquery-1.11.3.min.js'></script>
<script>
var xmlString;
try {
        //REST Query to get the display form url 
        jQuery.ajax(
        {
            url: "http://<URL>/_api/Web/Lists(guid'<GUID>')/Views(guid'<GUID>')",
            type: "GET",
            async: false,
            headers: { "Accept": "application/json;odata=verbose" },
            success: function (data, textStatus, xhr) {
                           xmlString = data.d.ListViewXml;
                            },
            error: function (data, textStatus, xhr) {
                alert("Error while getting Display form url");
            }
        });
    }
    catch (ex) {
        alert(ex);
    }

var xmlDoc =$.parseXML(xmlString);
$xml = $(xmlDoc);
$viewFields = $xml.find("ViewFields");
if($viewFields[0].childNodes.length > 0){

    for(var i=0; i < $viewFields[0].childNodes.length; i++){
        alert($viewFields[0].childNodes[i].attributes[0].textContent);
        }
}
</script>

Hope this helps to resolve your query!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.