This project has moved. For the latest updates, please go here.
1

Closed

SPGetCurrentUser not being async

description

From https://spservices.codeplex.com/discussions/463376

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
        $.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;
            }
        });
M.
Closed Dec 28, 2013 at 7:49 PM by sympmarc
Released in 2013.02

comments

sympmarc wrote Oct 24, 2013 at 1:59 PM

Implemented in 2013.02ALPHA4

sjaanus wrote Oct 30, 2013 at 10:16 AM

This should work, but when looking the code further, it gives me a undefined error:
$(thisUserDisp.responseText).find("table.ms-formtable td[id^='SPField']").each(function() {
                    if(thisTextValue.test($(this).html())) {
                        // Each fieldtype contains a different data type, as indicated by the id
                        switch($(this).attr("id")) {
                            case "SPFieldText":
                                thisField = $(this).text();
                                break;
                            case "SPFieldNote":
                                thisField = $(this).find("div").html();
                                break;
                            case "SPFieldURL":
                                thisField = $(this).find("img").attr("src");
                                break;
                            // Just in case
                            default:
                                thisField = $(this).text();
                                break;                      
                        }
                        // Stop looking; we're done
                        return false;
                    }
                });
It goes to that if clause, but thisUserDisp has not been defined yet.

sjaanus wrote Oct 30, 2013 at 10:17 AM

Exact error is this :

TypeError: Cannot read property 'responseText' of undefined

salascz wrote Jan 17, 2014 at 8:11 AM

Hi,
would it be possible add callback function (similar to completefunc(xData) in options for $().SPServices )?
It could help us in case of ASYNC call.

I would like to use async as much as possible. That is the reason, why I used JQuery Deferred/Promise solution. "Completefunc" seems to be a minimal requirement for this...

Of course - it will be better return JQueryPromise object (result from AJAX call) - but it will be confusing some people.

sympmarc wrote Jan 17, 2014 at 12:57 PM

SPServices already passes back promises, as 0f 2013.01.

Note: In the future, please start a new discussion rather than tagging onto the end of a closed ticket.

M.