Nisar
Nisar

Reputation: 6038

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

I am creating a list using Javascript. But while adding column to list it is getting error

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

Help me where i am making mistake

Upvotes: 1

Views: 1427

Answers (2)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59328

The example contains some errors/typos:

  • SP.FieldCollection.addFieldAsXml function accepts field xml (string type) as first parameter but you are passing Boolean(!) value from newCols array

    SP.ClientContext.load function accepts SP.ClientObject object but at line: context.load(numberCols); numberCols is a numeric value

Modified example

Below is provided a slightly modified version of your example:

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);
}

Usage

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

Upvotes: 1

jjczopek
jjczopek

Reputation: 3379

I can't test the code, but I see the following issues:

1) After adding your new list, you should load it:

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

2) Why you have true value in the array with the columns you would like to add? I suggest removing it. Or if you want to create one column, do as follows:

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

3) Instead of the loop, do this:

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

4) Call load method on the new column and execute query:

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

You were calling context.load(numberCols); where numberCols was length of the array with columns to be created - the code due the error did not reach that line, but it would fail here as well.

I hope that helps.

Upvotes: 1

Related Questions