1

I am trying to get list and Add fields into it using JSOM.
In code below I'm using $(function(){}) i.e. $(document).ready() function.

button id="addField"     
$(function () {
 $("#addField").click(function () {
    var clientContext = new SP.ClientContext();
    var oWebsite = clientContext.get_web();

    this.oList = oWebsite.get_lists().getByTitle('CustomList');
    this.oField = oList.get_fields().addFieldAsXml('<Field DisplayName=\'Product\' Type=\'Number\' />', true, SP.AddFieldOptions.defaultValue);

    var fieldNumber = clientContext.castTo(oField, SP.FieldNumber);
    fieldNumber.update();

    clientContext.load(oField);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
   });
function onQuerySucceeded() {
    var result = oField.get_title() + ' added.';
    alert(result);
}

function onQueryFailed() {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

function onQuerySucceeded() {
    var result = oList.get_title() + ' created.';
    alert(result);
}

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

So could I use ready function in JSOM code before bind any event.
If we can use $(function()) then give a sample code which would be useful.

  • What is the issue? Do you want any reference or what? api.jquery.com/ready – Ram Oct 17 '16 at 10:49
  • 2
    jQuery Ready function is only required if you want to update something in the DOM, if your script is executed before the DOM is available it can execute, just not do anything with DOM elements. – Danny '365CSI' Engelman Oct 17 '16 at 11:07
0

You can load that, but you'll need to wait until sp.js is loaded properly before using SP.Context. You can 'tee' up the function if you using the SP.SOD.executeFunc('sp.js', 'SP.ClientContext', yourFunction);

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.