0

We have SP2013 on site. Access is controlled through our active directory and therefore SP always knows who is using the page. In the upper right of the suite bar we get our name but none of the other expanded user info is there -- at least that I can find.

We have a lot of features -- contact us pages, other forms, discussions -- where we want to know details about the user.

I had added some javascript to request the user information and add it as a data node to the body. I used jquery to run this when the document was ready. But unfortunately in some cases the timing doesn't work right and other code which needs that information tries to run before the info comes back.

Pretty much everything I do is client side javascript and I don't know much about aspx pages and how to work them. Is there a way to add this directly to the masterpage and have it happen on the server side so I can count on the user info being there from the get go?

Edit

Part of the problem was I had document ready in a couple of spots and couldn't count on the order of them going. Here is the code I'm currently using which seems to be a bit better. (Sorry for the readability, I only have the unminified version...) My concern now is that the $.holdReady(true) might try and execute before jquery is available.

(function() {
    // tell jquery to not execute an $(document).ready()...
    $.holdReady(true);

    function e() {
        var e = new SP.ClientContext.get_current(),
            o = e.get_web();
        c = o.get_currentUser(), e.load(c), e.executeQueryAsync(n, t)
    }

    function n() {
        $.data(document.body, "currentUser", c);
        // got the values back $(document).ready() can go!
        $.holdReady(false);
    }

    function t(e, n) {
        // well no results, but lets not break everything else
        // let the rest of the $(document).ready() go
        $.holdReady(false);
    }
    var c;
    null != SP.ClientContext ? SP.SOD.executeOrDelayUntilScriptLoaded(e, "SP.js") : SP.SOD.executeFunc("sp.js", null, e)
})();

1 Answer 1

0

That is because JavaScript is asynchronous by nature and it's not guaranteed to load in the same order it is defined in the page.

If you are making a request using jQuery, you could add an async:false parameter to load it in the same order it is defined though this could have serious performance issues.

Another option is to use JavaScript promises to handle the asynchronicity.

5
  • Thanks. I'm not using jQuery to request the user info and I don't know if there is any way to tell get_currentUser() to be synchronous. I was really hoping there was a way to have it happen server side. It just kinda blows my mind that OOTB the user name is displayed on every page, but the user info isn't available on every page! It isn't in _spPageContextInfo or anywhere.
    – Rothrock
    Commented Oct 21, 2015 at 18:38
  • Since you are using jQuery anyway, you could just execute an AJAX REST call to siteurl/_api/web/currentuser. This will also give you the user info and you can set async:false to make it synchronous. You can refactor your code to include promises to make sure the data is returned. Not aware of a server side solution except XSLT. What type of user info are you trying to retrieve?
    – Akhoy
    Commented Oct 21, 2015 at 18:44
  • Trying to get user name, email, and user id. The name appears in the suitebar, but it just seems hacky to use $('#DeltaSuiteBarRight').text() and then try and extract the name from the nameSHIFT+ENTER to Open the menu (new window).
    – Rothrock
    Commented Oct 21, 2015 at 19:37
  • JS would be a preferred solution here rather than some server side hack. You can either make a call to the above mentioned REST url (thereby making it synchronous by adding async:false) which is much easier and will reduce the functions that you're using currently. Or, you can add promises to your current code like in the link mentioned in my answer.
    – Akhoy
    Commented Oct 21, 2015 at 19:48
  • So how would I do the REST request? It looks like ...site/_api/web/currentUser is the url, but I've never done anything with REST and don't know how to deal with that. I would prefer not to have to first wait for jquery to load....
    – Rothrock
    Commented Oct 27, 2015 at 18:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.