1

I'm using the SharePoint 2013 JavaScript APIs, calling web.get_lists().getByTitle('ListTitle') and then list.get_fields() to create a custom search panel. However, the fields do not come back based on the column ordering set in the list settings. Is there a way to get to the column order value via JavaScript? I've seen examples for setting, but none for reading it. Thanks in advance for your help.

1

There is no such property in field object, fields are sorted in List Settings page by title if Content Types are enabled as demonstrates the following example.

Example

function getListFields(listTitle,success,failure)
{
     var ctx = SP.ClientContext.get_current(); 
     var list = ctx.get_web().get_lists().getByTitle(listTitle); 
     var fields = list.get_fields()
     ctx.load(fields);
     ctx.load(list);
     ctx.executeQueryAsync(
        function(){
            var listFields = [];
             for(var i = 0; i < fields.get_count();i++) {
                 var field = fields.get_item(i);
                 if(field.get_hidden() == false && field.get_typeAsString() != 'File' 
                    && field.get_typeAsString() != 'Computed' && field.get_typeAsString() != 'Counter' 
                    || (field.get_typeAsString() == 'Lookup' && field.get_readOnlyField() != true)) {
                    listFields.push(field);   
                 }
            }
            if(list.get_contentTypesEnabled())
               listFields.sort(fieldComparer); //sort fields
            success(listFields);
        },
        failure);
}


function fieldComparer(x,y) {
  if (x.get_title() < y.get_title())
     return -1;
  if (x.get_title() > y.get_title())
    return 1;
  return 0;
}

Usage

getListFields('Pages',
function(fields){
    for(var i = 0; i < fields.length;i++) {
        console.log(fields[i].get_title());
    }
},
function(sender,args){
    console.log(args.get_message());
});

P.S. In the provided example the fields of type Computed, File and Counter are excluded since they are not displayed in List Settings page.

0

I guess you would need to get the View for the lists, rather than the columns of the list. That way the ordering will be in place.

Look here: using CSOM to retrieve fields in a list in a view

  • Thank you dotsa. That could work. I was hoping to not have to rely on a view but rather get the values from the list settings column order. But if there are no other suggestions I will go that route. Appreciate the link to the sample. Thanks again! – user1100272 Mar 24 '15 at 21:03
0

For anyone interested, I ended up creating a view on each list (configSearch) I am building dynamic advanced filtering for. The view allows me/user to layout the fields in any order and accommodate the adding of new fields to the search pane as the list grows. When a user select a list to search, I pass the list name to the following function. Once the data is returned I get the order from the view fields and the field details from list fields. Having that data I implement rendering logic for each field type (not included here). Later filtering is done using CamlJs (codeplex - not included here).

function getListFields(listnm) {
var clientContext = SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle(listnm);
this.searchlist = list;
this.listFields = list.get_fields();
this.configFields = list.get_views().getByTitle("configSearch").get_viewFields();
clientContext.load(this.searchlist);
clientContext.load(this.listFields);
clientContext.load(this.configFields);    
clientContext.executeQueryAsync(Function.createDelegate(this, 
    this.onListFieldsQuerySucceeded), Function.createDelegate(this, 
    this.onListFieldsQueryFailed));
}

function onListFieldsQuerySucceeded() {
var fieldEnumerator = listFields.getEnumerator();
var fields = [];
while (fieldEnumerator.moveNext()) {
    var listField = fieldEnumerator.get_current();
    if (listField.get_hidden() || (listField.get_schemaXml().indexOf('http://') > -1 && listField.get_staticName().indexOf('Title') === -1))//skip sys fields
        continue;

    fields.push(listField);
    }

    $(window.configFields.get_data()).each(function (findex,fldIntNm) {//loop through view fields
        var oField = $(fields).filter( function () { if (this.get_internalName() == fldIntNm) return this; })[0];  //get field config info to help with rendering logic
             var fType = oField.get_fieldTypeKind();
             var fStaticName = oField.get_staticName();
             var fTitle = oField.get_title();
        //do more here - rendering logic
    });

});
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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