使用Javascript检索Sharepoint列表的字段/列名称

时间:2014-09-04 05:10:21

标签: javascript jquery sharepoint-2013 sharepoint-designer

<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>

我想使用javascript显示视图的字段。

注意:我的列表名称是“pranav_list”,视图是“Main”。

帮助..!

1 个答案:

答案 0 :(得分:1)

SP.View.viewFields property返回字段名称,但不返回Field客户端对象集合。

以下示例演示如何从视图中打印字段名称:

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 viewFields = view.get_viewFields();
   context.load(viewFields);
   context.executeQueryAsync(printFieldNames,onError);


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

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

}