I am using JSOM to create list and add fields to it
createList("List Name");
setTimeout(function () { addFieldToList("List Name", "<Field Type='Text' Name='Link' StaticName='Link' DisplayName='Link' />"); }, 3000);
setTimeout(function () { addFieldToList("List Name", "<Field Type='Number' Name='DisplayOrder' StaticName='DisplayOrder' DisplayName = 'DisplayOrder' /> "); }, 4000);
setTimeout(function () { addFieldToList("List Name", "<Field Type='Choice' DisplayName='IsActive' Name='IsActive' StaticName='IsActive' Format = 'Dropdown' > <Default>Yes</Default><CHOICES> <CHOICE>Yes</CHOICE> <CHOICE>No</CHOICE></CHOICES></Field>"); }, 5000);
setTimeout(function () { addFieldToList("List Name", "<Field Type='Choice' DisplayName='NewTab' Name='NewTab' StaticName='NewTab' Format = 'Dropdown' > <Default>No</Default><CHOICES> <CHOICE>Yes</CHOICE> <CHOICE>No</CHOICE></CHOICES></Field>"); }, 6000);
setTimeout(function () { createListItem("List Name", "Overview"); }, 7000);
setTimeout(function () { createListItem("List Name", "Policies"); }, 7500);
setTimeout(function () { createListItem("List Name", "Mangement"); }, 8000);
This is not working consistently. Many times, this works successfully, but sometimes it is not able to create all the fields
function addFieldToList(listname, fieldxml) {
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var clientContext = new SP.ClientContext(siteUrl);
$(".col-ms-4.one").show().html("adding fields..");
var oList = clientContext.get_web().get_lists().getByTitle(listname);
this.oField = oList.get_fields().addFieldAsXml(
fieldxml,
true,
SP.AddFieldOptions.defaultValue
);
clientContext.load(oField);
clientContext.executeQueryAsync(
Function.createDelegate(this, function () {
var result = oField.get_title() + ' added.';
console.log(result);
}),
Function.createDelegate(this, function () { console.log("not able to add fields") })
);
}
The problem is, this runs without any issues sometimes and its not creating fields sometimes. Any insight on making it work consistently would be appreciated