Sharepoint - Uncaught TypeError: Cannot set property 'newColumns' of undefined

Sharepoint - Uncaught TypeError: Cannot set property 'newColumns' of undefined

我正在使用 Javascript 创建列表。但是在将列添加到列表时出现错误

function createlist() {
    // Create a generic SharePoint list with the name that the user specifies.
    var listCreationInfo = new SP.ListCreationInformation();
    var listTitle = "MyList";
    listCreationInfo.set_title(listTitle);
    listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
    lists = web.get_lists();
    var newList = lists.add(listCreationInfo);

    var newCols = [  "<Field Name='PageText' DisplayName='Page Text' Type='Note' RichText='TRUE'   RichTextMode='FullHtml' NumLines='10' />",
    true];

    var numberCols = newCols.length;
     for (var i = 0; i < numberCols; i++) {
            this.newColumns= newList.get_fields().addFieldAsXml(newCols[i],true,SP.AddFieldOptions.defaultValue);
     }


    context.load(numberCols);
    context.executeQueryAsync(onListCreationSuccess, onListCreationFail);
}

Uncaught TypeError: Cannot set property 'newColumns' of undefined

createlist @ App.js:57

(anonymous function) @ App.js:18

b.Callbacks.c @ jquery-1.9.1.min.js:3

b.Callbacks.p.fireWith @ jquery-1.9.1.min.js:3

b.extend.ready @ jquery-1.9.1.min.js:3 H @ jquery-1.9.1.min.js:3

帮帮我哪里出错了

我无法测试代码,但我发现了以下问题:

1) 添加新列表后,您应该加载它:

var newList = lists.add(listCreationInfo);
context.load(newList);

2) 为什么要添加的列在数组中有真值?我建议删除它。或者,如果你想创建一列,请执行以下操作:

var newCol = "<Field Name='PageText' DisplayName='Page Text' Type='Note' RichText='TRUE'   RichTextMode='FullHtml' NumLines='10' />";

3) 代替循环,这样做:

var newColumn = newList.get_fields().addFieldAsXml(newCol,true,SP.AddFieldOptions.defaultValue);

4) 在新列上调用加载方法并执行查询:

context.load(newColumn);
context.executeQueryAsync(onListCreationSuccess, onListCreationFail);

您正在调用 context.load(numberCols);,其中 numberCols 是包含要创建的列的数组的长度 - 由于错误而导致的代码没有到达该行,但它也会在这里失败。

希望对您有所帮助。

示例包含一些errors/typos:

  • SP.FieldCollection.addFieldAsXml 函数接受字段 xml (string type) 作为第一个参数,但您要从 newCols 数组传递布尔值 (!)

    SP.ClientContext.load 函数接受 SP.ClientObject 对象,但在行:context.load(numberCols); numberCols 是一个 数值

修改示例

下面提供了您的示例的略微修改版本:

function createlist(listTitle,fieldDefs,success,error) {

    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(listTitle);
    listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
    var list = web.get_lists().add(listCreationInfo);
    var fields = [];
    for (var i = 0; i < fieldDefs.length; i++) {
         var field = list.get_fields().addFieldAsXml(fieldDefs[i],true,SP.AddFieldOptions.defaultValue);
         fields.push(field);
         ctx.load(field);
    }
    ctx.executeQueryAsync(
        function(){
            success(fields);
        }, error);
}

用法

var listTitle = 'Requests';
var fieldDefs = [  "<Field Name='RequestDesc' DisplayName='Request Description' Type='Note' RichText='TRUE'   RichTextMode='FullHtml' NumLines='10' />"];

createlist(listTitle,fieldDefs,
  function(fields){
      console.log('List has been created successfully');
  },
  function(sender,args)
  {
      console.log(args.get_message()); 
  });