1

I am tryting to create module pattern with ExecuteQueryAsync like this:

    UsersMngt = (function () {

        getUserIdByEmail = function(email){

        var clientContext = new SP.ClientContext.get_current();
        var web = clientContext.get_web();

        var user = web.get_siteUsers().getByEmail(email);


        clientContext.load(user);

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));


        clientContext.executeQueryAsync(alert('yes'),alert('no'));               
}; //getUserIdByEmail




return{
    GetUserIdByEmail:getUserIdByEmail,
};

})();

and then I want to call it like:

UsersMngt.GetUserIdByEmail('user.name@email.com')

but, everytime I call it, with or without success, it always gives me both "yes" and "no" (success and failure). It also retrieves data I need in case of succces.

How can I include functions directly in "executeQueryAsync" without creating actually delegates somewhere else? I would like everything to be kept within the same code structure.

So far, it only works with delegates.

Thank you for ideas and help

  • If an answer was helpful could you remove your question from the SO unanswered list, by marking it as answered, tnx – Danny '365CSI' Engelman Jun 4 '17 at 7:56
  • Thanks, will do. I wanted to do it originally but due to rules of this site I had to wait two days and then it slipped my mind:) – Pedro Jun 4 '17 at 7:58
0

Once this clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); query is executed, it will call the onQuerySucceeded or onQueryFailed functions depending on the result of the query.

I advice you to call the executeQueryAsync just one. and write alert messages inside onQuerySucceeded or onQueryFailed functions.

Also createDelegate is too old now. Using just this would be fine:

clientContext.executeQueryAsync(onQuerySucceeded,onQueryFailed);
  • For simplicity one executeQueryAsync is helpful; but you can execute more: You, get me a Coffee!, You! get me a bagel You! get me the newspaper Three asynchronous commands... maybe like: Update Item, Update ParentItem, Trigger workflow Do read how to pass variables to a function – Danny '365CSI' Engelman May 17 '17 at 10:09
0

This code executes asynchronous

code does not stop when executed, the Success or Error Callback Functions execute when the Async operation is done.

It is like telling someone else to get you a coffee

What you now program

clientContext.executeQueryAsync(alert('yes'),alert('no'));     

Is: you send someone to get a coffee, and before he leaves he says "Yes"

The parameters you have to supply are Function Definitions so without the ( ),
not Function Calls you do now

That whole Function.createDelegate is old Microsoft IE8 stuff,
ditch and learn modern IE9 notation: Differing ways to make executeQueryAsync calls - Function.createDelegate (is old IE8 code)

More details: Looping jsom getItems

0

thank you, I solved it like this, because I wanted to use annonymous functions:

UsersMngt = (function () {

getUserIdByEmail = function(email){

        var clientContext = new SP.ClientContext.get_current();
        var web = clientContext.get_web();

        var user = web.get_siteUsers().getByEmail(email);


        clientContext.load(user);


        clientContext.executeQueryAsync(
            function (){
                    alert("yes")
            },
            function(){
                    alert("no")
            }            
        )}; //getUserIdByEmail


return{
    GetUserIdByEmail:getUserIdByEmail,
};

})();

  • Good, and since the declaration (they might be ananymous, but still exist) is inside the getUserIdByEmail function scope, they have access to all variables from that scope. That eleviates the need for oldSkool Function.createDelegate or modern .bind() But be carefull!! it is async code so your error/success function will execute at a point in time where variables Could have changed. Non-anonymous but referenced Functions are saver to use, as you can never access the calling functions scope (unless you use bind() to make a copy of a Scope) so less prone to logic errors – Danny '365CSI' Engelman May 17 '17 at 13:56
  • Hi Danny, thank you. I used Deffered object in this function in the end so I can check for success or fail and it works like a charm! :) thank you – Pedro May 17 '17 at 14:14

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.