3

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

  • Because SP is sometimes soo slow that setTimeout will trigger too soon; learn to use cascacding Callbacks (an while you are at it, ditch that IE8 createDelegate code) – Danny '365CSI' Engelman Jun 6 '17 at 6:45
5

Get rid of this and Function.createDelegate.

Use your code as below:

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);
    var oField = oList.get_fields().addFieldAsXml(fieldxml,true,SP.AddFieldOptions.defaultValue);

    clientContext.load(oField);
    clientContext.executeQueryAsync(function() {
           var result = oField.get_title() + ' added.';
           console.log(result);
        },function(){ 
            console.log("not able to add fields") 
        });
}

Also, timeout is a really bad way to get things done. You should use promises to make it a smoother.

Dummy code would be somewhat as :

$.when(addFieldToList("List Name", "<Field  Type='Text' Name='Link' StaticName='Link' DisplayName='Link' />")) 
.done(function (data) {   
  addFieldToList("List Name", "<Field  Type='Number' Name='DisplayOrder' StaticName='DisplayOrder' DisplayName = 'DisplayOrder' /> ").done(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>").done(function(){
        //do something here
    })
  })
}) 
.fail(function (sender, args) { 
  // To do something 
}); 

Using Promises with JSOM

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.