0

I am using jsom to create a list. ExecuteQueryAsync returns a failure (onFaildeCallBack) but the list was created successfully. Am I doing something wrong?

<button onclick="createListEmployee()">Click here to Create a List (createListEmployee)</button>

<script type='text/javascript'>

function ListCreation(spListCreationInformation){
    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();

    //Set the list values
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(spListCreationInformation.listName);
    listCreationInfo.set_templateType(spListCreationInformation.listType);
    listCreationInfo.set_description(spListCreationInformation.listDescription);

    this.olist = oWebsite.get_lists().add(listCreationInfo);
    clientContext.load(olist);

    //Set the field values for the list
    var index;
    for (index = 0; index < spListCreationInformation.listFields.length; ++index) {
        console.log(spListCreationInformation.listFields[index]);
        this.olist.get_fields().addFieldAsXml(spListCreationInformation.listFields[index],true,SP.AddFieldOptions.defaultValue);
    }

    // create the list and fields
   clientContext.executeQueryAsync(
      Function.createDelegate(this,this.onSucceedeCallback),
      Function.createDelegate(this,this.onFaildeCallBack));
}

function onSucceedeCallback(sender,args) {
    var result = oList.get_title() + ' created.';
    alert(result);
}

function onFaildeCallBack(sender,args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_errorCode());
}


function createListEmployee(){
    //Specify the list
    var spListCreationInformation = new Object();
    spListCreationInformation.listName = "Employee";
    spListCreationInformation.listDescription = "List which contains the Employee information";
    spListCreationInformation.listType = SP.ListTemplateType.genericList;
    spListCreationInformation.listFields = [];

    //Specify the fileds for the list
    var spFieldInformation;
    spFieldInformation = '<Field Type="Text" DisplayName="NewField1" Name="NewField1"/>';
    spListCreationInformation.listFields.push(spFieldInformation);

    spFieldInformation = '<Field Type="Text" DisplayName="NewField2" Name="NewField2"/>';
    spListCreationInformation.listFields.push(spFieldInformation);

    ListCreation(spListCreationInformation);

}

</script>

enter image description here

0

Your for loop is doing operations on a list that does not exist yet because you execute the create query async

1
  • Well even without the for loop I do get the same error. As mentioned above even that I get the error the list and the fields gets created. So in general the script is running fine...
    – Lutz Beyer
    Oct 17 '16 at 20:37
0

I found the issue.

All I need to do is to change

<button onclick="createListEmployee()">Click here to Create a List (createListEmployee)</button>

into

<button type="button" onclick="createListEmployee()">Click here to Create a List (createListEmployee)</button>

So I just added

type="button"

an everything is working perfectly.

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.