0

I am currently working on a method to resolve multiple users in a user picker field in my sharepoint app.

For this I need the elementId which is supported by the OnValueChangedClientScript function from SharePoint in the callback function which will then inserts the received data in the element with the specific elementId.

But I know that there is no possibility to serve specific data for callback functions as normal function parameters.

So is there a way to add some data to the Rest API which will then be silently sent back for custom function redirection afterwards?

Or can I build something like this on my own?

EDIT: this is the function with which I am requesting the userID: getUserId: function (loginName, callback) {

    var executor = new SP.RequestExecutor(decodeURIComponent(AppCoreFunctions.getAppRelativeUrls().appweburl));
    var url = AppCoreFunctions.getAppRelativeUrls().appweburl + "/_api/SP.AppContextSite(@target)/web/siteusers(@v)?@v='" + SP.Utilities.HttpUtility.urlKeyValueEncode(loginName) + "'&@target='" + AppCoreFunctions.getAppRelativeUrls().hostweburl + "'";
    executor.executeAsync({
        url: url,
        method: "GET",
        headers: { "accept": "application/json; odata=verbose" },
        success: function (data) {
            var JSONData = JSON.parse(data["body"]);
            //<--- I want to use the elementId of the UserPicker-Field here!
            callback(JSONData);
        },
        error: function (sender, args) {
            console.log(sender);
        }
    });
}
-1

no possibility to serve specific data for callback functions as normal function parameters

Unless you know some more JavaScript, and understand what this is
Besides Parameters there is this thing called context execution (or various other terms)

That means you can tell your callback Function to work in another scope

pseudocode:

function callbackFunc(data){
  var myData = this;  // ► 'this' is what I told it to be
  console.log('elementId is ', myData.elementId);
}

var myCallbackData={
    var elementId='X';
};
FancyAsyncRESTcall({//or jQUery.$ajax

   success : callbackFunc.bind(myCallbackData) // ► 'this' is what I tell it to be

});

Also read why Function.createDelegate (you see in many answers) does the same but is outdated IE8 SharePoint code:

Differing ways to make executeQueryAsync calls - Function.createDelegate (is old IE8 code)

Note: if you have beginning TypeScript developers in your team who do not understand JavaScript, you can really confuse them with this..

Re: OPs code addition

You can bind the function declaration:

    success: function (data) {
        var JSONData = JSON.parse(data["body"]);
        //<--- I want to use the elementId of the UserPicker-Field here!
        callback(JSONData);
    }.bind(WHATEVERYOUWANTTOBETHESSCOPE),
  • but I have no static callback function. I just provide a function name and call it. so there is also no static variable. I am processing the SP Response on the fly. – Snickbrack Nov 11 '16 at 13:18
  • You have a "static" function just not named it. – Danny '365CSI' Engelman Nov 11 '16 at 13:33
  • Note: instead of replacing the scope you could also use the default scope and stick your data in there, but if you copy/paste codefragments it is less obvious what is going on – Danny '365CSI' Engelman Nov 11 '16 at 13:36

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.