2015-09-10 128 views
1

我正在使用Javascript創建一個列表。但同時加入欄列出它越來越錯誤Sharepoint - Uncaught TypeError:無法設置屬性'newColumns'undefined

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

的例子包含某些錯誤/錯字

  • 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()); 
    }); 
1

我無法測試的代碼,但我看到了以下問題:

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是要創建的列數組的長度 - 由於錯誤的代碼沒有達到該行,但它會在這裏失敗,因爲我們二。

我希望有幫助。

相關問題