1
<script type="text/javascript">
  function retrieveFieldsOfListView(){

var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle('pranav_list');
var view = list.get_views().getByTitle('Main');
this.listFields = view.get_viewFields();
clientContext.load(this.listFields);
clientContext.executeQueryAsync(Function.createDelegate(this, 
    this.onListFieldsQuerySucceeded9), Function.createDelegate(this, 
    this.onListFieldsQueryFailed));
}
function onListFieldsQuerySucceeded9() {
var fieldsinfo='';
    var fieldEnumerator = listFields.getEnumerator();
    while (fieldEnumerator.moveNext()) {
        var oField = fieldEnumerator.get_current();
        var fType = oField.get_fieldTypeKind();
        fieldsinfo +='\n '+oField.get_title();
    }
    alert(fieldsinfo);
    }
</script>

I want to show the fields of the view using javascript.

Note: My list name is "pranav_list" and view is "Main".

Help..!

4
  • So you are not getting fields in listFields?
    – Aanchal
    Commented Sep 4, 2014 at 5:38
  • Yes...not getting the fields..:( Commented Sep 4, 2014 at 6:09
  • So "Main" is the name of your custom view??
    – Aanchal
    Commented Sep 4, 2014 at 6:15
  • Yes..."Main" is View name Commented Sep 4, 2014 at 6:15

1 Answer 1

3

SP.View.viewFields property returns field names but not a Field client objects. The following example demonstrates how to print field names in View.

Working example:

function retrieveFieldsOfListView(listTitle,viewName){

   var context = new SP.ClientContext.get_current();
   var web = context.get_web();
   var list = web.get_lists().getByTitle(listTitle);
   var view = list.get_views().getByTitle(viewName);
   var listFields = view.get_viewFields();
   context.load(listFields);
   context.executeQueryAsync(printFieldNames,onError);


   function printFieldNames() {
      var e = listFields.getEnumerator();
      while (e.moveNext()) {
         var fieldName = e.get_current();
         console.log(fieldName);
      }
   }

   function onError(sender,args)
   {
      console.log(args.get_message());
   }

}
2

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.