0

i have many functions that read informations from SharePoint lists and write informations in SharePoint lists. These functions depent on each other meaning for example, in the first function i write information in the list. After that first function i call the secound function that reads the writen informations.

To read and write informations i use executeQueryAsync.

My problem is because of the async the data gets read before the first function can write it in the list.

In the moment i work with timeouts and hope the informations are written down when the timeout ends, then i read them. But this is not a very good solution. Is there a way i can execute the functions that work with executeQueryAsync one by another.

Example:

functionA(); // writes information in list A
functionB(); // reads and write information from list A
functionC(); // reads information from list A

How can i tell the functions to wait for each other? Only if a function is fully executed the next functions starts.

Does anyone have an idea?

Many thanks!

3 Answers 3

1

By design SP.ClientContext.executeQueryAsync function accepts two parameters:

  • succeededCallback A function or a delegate of the method to call if the request executes successfully.
  • failedCallback A function or a delegate of the method to call if the request fails to execute.

Given that fact, you could consider the following approach to execute the next function once the previous one is completed:

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle('Announcements'); 
var properties = {'Title' : 'New announcement',
              'Body' :  'Welcome to a new site'};

var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
for(var name in itemProperties)    
      listItem.set_item(name, itemProperties[name]);
listItem.update();
ctx.load(listItem);
ctx.executeQueryAsync(
   function(){
       //execute next function here... 
   },
   function(sender,args){
    console.log(args.get_message());
});

Alternatively you could consider to execute functions in a chainable manner via Promise, for example using jQuery.Deferred as demonstrated below

function executeQueryPromise(ctx, result) {
    result = result || {};
    var d = jQuery.Deferred();
    ctx.executeQueryAsync(function () {
         d.resolve(result);
    }, function (sender, args) {
         d.reject(args);
    });
    return d.promise();
}

Usage

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle('Announcements');

var properties = {'Title' : 'New announcement',
                  'Body' :  'Welcome to a new site'};
createListItem(list,properties)
.then(function(item){
    return readListItems(list);
},logError)
.then(function(items){
    console.log("Done");
},logError);

where

function logError(sender,args){
    console.log(args.get_message());
}

function createListItem(list,itemProperties){
    var ctx = list.get_context();
    var itemCreateInfo = new SP.ListItemCreationInformation();
    var listItem = list.addItem(itemCreateInfo);
    for(var name in itemProperties)    
          listItem.set_item(name, itemProperties[name]);
    listItem.update();
    ctx.load(listItem);
    return executeQueryPromise(ctx,listItem);
}

function readListItems(list){
    var ctx = list.get_context();
    var listItems = list.getItems(SP.CamlQuery.createAllItemsQuery());
    ctx.load(listItems);
    return executeQueryPromise(ctx,listItems);
}
Sign up to request clarification or add additional context in comments.

1 Comment

i have trouble understanding your code a bit. can you see my comment about that?
0

Try nesting the function calls within the success() methods of the previous call. That will ensure that the functions will not execute until the previous one completes.

Comments

0

To handle the async call in a sequential manner, there is a provision given by jquery: Deferred ( factory function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.)

reference link: https://api.jquery.com/jquery.deferred/ http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.