1

Code to create list(working :) )

    var lists;

    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title('List1'); // list name
    listCreationInfo.set_templateType(SP.ListTemplateType.genericList); //list type

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

    .
    .
    executeQueryAsync

Code to add field in list(may need rework!)

    var oField = list.get_fields().addFieldAsXml('<Field DisplayName=\'Key\' Type=\'Text\' />', true, SP.AddFieldOptions.defaultValue);
    var field1 = context.castTo(oField, SP.text);
    field1.update();
    context.load(oField1);
    .
    .
    executeQueryAsync

How can I create a list with columns in 1 go, without query execute multiple times?

1

Have you tried just removing the executeQueryAsync in your first example, and immediately performing the actions in the 2nd example you posted, leaving only the one executeQueryAsync at the end?

As you are using the same list object reference the queries will be run in sequence even though the list object has not yet been executed / instantiated.

context.load(..) can be called multiple times (up to 256 query objects I think) and the executeQuery executes all synchronously.

1

You will need 2 ExecuteQueryAsync requests. One for creating the list, and another for creating the columns and updating the list. However if you wanted to create the list and update list properties like description, it could be done with 1 ExecuteQueryAsync

0

You would have to either do two ExecuteQueryAsync's or you would have to create a custom template that has the columns you need already and call to create that instead of the genericlist above as described here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.