SPGetCurrentUser not being async

Oct 24, 2013 at 9:25 AM
I have a problem, that all the users are always waiting after this call. It takes up to 10 seconds sometimes. I tried adding the async:true parameter, but still no effect.

How can I make this call async, or which call I should use, to get the current username?
var user = $().SPServices.SPGetCurrentUser({
            fieldName: "Name",
            async: true
Oct 24, 2013 at 12:38 PM
If you check the docs, you'll see that SPGetCurrentUser loads the /_layouts/userdisp.aspx page to scrape the values. It's not going to pay attention to the async setting.

The user account is usually in the page somewhere else, depending on what you are trying to do, for instance in the Welcome control. In other words, there are alternatives for specific use cases.

Finally, that 10 seconds points to the fact that you have a slow farm. My guess is that your overall performance isn't good, either. It would make sense to look into that as well.

Oct 24, 2013 at 12:44 PM
couldn't it scrape the info in async mode?

since it says : "The SPGetCurrentUser function does an AJAX call"
AJAX calls should be available to do in async.

you could be true about the slow farm, but thats the reason I would like to async, so it wouldn't have to wait behind the slow bottleneck.
Oct 24, 2013 at 12:57 PM
Edited Oct 24, 2013 at 12:58 PM
It could be async, but the function is simple, and I don't want to assume that people understand program flow with this one.

That said, I should respect the value of async if it's passed in. I'll do a fix for the next release. In the meantime, you could patch your version:
    // Function which returns the account name for the current user in DOMAIN\username format
    $.fn.SPServices.SPGetCurrentUser = function(options) {

        var opt = $.extend({}, {
            webURL: "",             // URL of the target Site Collection.  If not specified, the current Web is used.
            fieldName: "Name",      // Specifies which field to return from the userdisp.aspx page
            fieldNames: {},         // Specifies which fields to return from the userdisp.aspx page - added in v0.7.2 to allow multiple columns
            debug: false,           // If true, show error messages; if false, run silent
            async: false
        }, options);

        // The current user's ID is reliably available in an existing JavaScript variable
        if(opt.fieldName === "ID" && typeof SPServicesContext.thisUserId !== "undefined") {
            return SPServicesContext.thisUserId;

        var thisField = "";
        var theseFields = {};
        var fieldCount = opt.fieldNames.length > 0 ? opt.fieldNames.length : 1;
        var thisUserDisp;
        var thisWeb = opt.webURL.length > 0 ? opt.webURL : $().SPServices.SPGetCurrentSite();

        // Get the UserDisp.aspx page using AJAX
            // Need this to be synchronous so we're assured of a valid value
            async: opt.async,
            // Force parameter forces redirection to a page that displays the information as stored in the UserInfo table rather than My Site.
            // Adding the extra Query String parameter with the current date/time forces the server to view this as a new request.
            url: thisWeb + "/_layouts/userdisp.aspx?Force=True&" + new Date().getTime(),
            complete: function (xData) {
                thisUserDisp = xData;
Oct 24, 2013 at 12:57 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.