SPGetCurrentUser not returning user information

Dec 19, 2011 at 9:13 PM

First, thank you so much for SPServices! CascadeDropDowns and ArrangeChoices made my life so much easier. I'd like to implement SPGetCurrentUser in a website but I'm not able to get the first piece to work -- simply return the information in an alert. My code is :

<script language="javascript" type="text/javascript" src="../../scripts/jquery-1.7.min.js"></script>
<script language="javascript" type="text/javascript" src="../../scripts/jquery.SPServices-0.6.2.min.js"></script>
<script type="text/javascript">
    var userName = "";
    $(document).ready(function()
       {
           alert(“ready”);
           userName= $().SPServices.SPGetCurrentUser({fieldName: “Title”}
       );
       alert(userName);
    });
</script>

I've tested that the scripts urls are pointing to the correct scripts and in the body I can get document.write to write "Hello World", I thought maybe pop-ups were disabled, but that isn't the case either. Also, I've checked _layouts/userdisp.aspx?Force=True and it does display the information about the user.

I want to use the information retured on the user to select a value in a drop-down list. The site has two drop-downs: Manager and Contractor. When the Manager's list is selected, the Contractor's list displays the contractors who report to that manager. I want the Manager list to be selected to the user who accesses the site. For example, if Jack Jones access the site, he sees his name selected automatically with the list of contractors who reports to him in the second drop down. If I can get this to work (return the information), my next challenge will be how to wire it into the Manager drop-down list.

Thank you for any help/advice you can give!

Dec 19, 2011 at 9:32 PM
Joni76,
This may not be the issue, but I did notice you are pointing to jQuery 1.7. You need to use SPServices 0.7. Jquery 1.7 introduced changes that broke SPServices 0.6.x.

Paul

___
Paul

Sent from mobile device.


On Dec 19, 2011, at 4:13 PM, joni76 <notifications@codeplex.com> wrote:

From: joni76

First, thank you so much for SPServices! CascadeDropDowns and ArrangeChoices made my life so much easier. I'd like to implement SPGetCurrentUser in a website but I'm not able to get the first piece to work -- simply return the information in an alert. My code is :

<script language="javascript" type="text/javascript" src="../../scripts/jquery-1.7.min.js"></script>
<script language="javascript" type="text/javascript" src="../../scripts/jquery.SPServices-0.6.2.min.js"></script>
<script type="text/javascript">
var userName = "";
$(document).ready(function()
{
alert(“ready”);
userName= $().SPServices.SPGetCurrentUser({fieldName: “Title”}
);
alert(userName);
});
</script>

I've tested that the scripts urls are pointing to the correct scripts and in the body I can get document.write to write "Hello World", I thought maybe pop-ups were disabled, but that isn't the case either. Also, I've checked _layouts/userdisp.aspx?Force=True and it does display the information about the user.

I want to use the information retured on the user to select a value in a drop-down list. The site has two drop-downs: Manager and Contractor. When the Manager's list is selected, the Contractor's list displays the contractors who report to that manager. I want the Manager list to be selected to the user who accesses the site. For example, if Jack Jones access the site, he sees his name selected automatically with the list of contractors who reports to him in the second drop down. If I can get this to work (return the information), my next challenge will be how to wire it into the Manager drop-down list.

Thank you for any help/advice you can give!

Dec 20, 2011 at 1:51 PM

Thank you for such a quick reply, Paul. I am now pointing to jquery-1.7.1.min.js and jquery.SPServices-0.7.0.min.js. However, I still have the same results -- not returning the user information. Any other thoughts or ideas will be greatly appreciated.

Joni

Coordinator
Dec 20, 2011 at 1:58 PM
Edited Dec 20, 2011 at 1:59 PM

What are you seeing in the alert of userName? I'm guessing it's blank? Have you tried alerting SPGetCurrentUser directly?

alert($().SPServices.SPGetCurrentUser({fieldName: “Title”}));

M.

Dec 20, 2011 at 2:28 PM

Here's some interesting behavior for you. :) I can document.write "Hello World" and it shows perfectly. I can alert "Hello World" and the alert opens with the message "Hello World". I replace the Hello World with $().SPServices.SPGetCurrentUser({fieldName: “Title”}) and even the Hello World doesn't work. If I comment out the lines with SPGetCurrentUSer, Hello World works.

<script type="text/javascript">
    document.write ("Hello World");
    alert("Hello World");
    document.write ($().SPServices.SPGetCurrentUser({fieldName: “Title”}));
    alert($().SPServices.SPGetCurrentUser({fieldName: “Title”}));
</script>

This code is in the body of the document, not the head. correct?

 

Coordinator
Dec 20, 2011 at 2:31 PM

It might be from the copy and paste, but it looks like you have the wrong type of quotes. Can you check that? “ versus ". (Yes\, I know that is almost impossible to distinguish visually.) If you copy and paste from a Web page, that often happens.

M.

Dec 20, 2011 at 2:36 PM

YES! SUCCESS! I replaced the double quote with a single quote and it now returns my user information. THANK YOU! Now all I have to do is figure out how to tie it into the list to make it select the user. The best Holiday Present ever! :)

Coordinator
Dec 20, 2011 at 2:38 PM

Yay! Always be careful copy and pasting code from Web pages, as you often get those &ldquo and &rdquo characters rather than &quot.

M.

May 6, 2013 at 1:04 PM
Hello people. Sorry to bring this conversation up again, but I have the same problem, not solved with the current solution (regarding quotes).

Any other reason that can make the function not return any value?

var userName = $().SPServices.SPGetCurrentUser({
fieldName: "Name",
debug: true
});

if I have this inside an alert or use the var, it doesn't do anything!! how can I debug it?

Thanks in advance
Coordinator
May 6, 2013 at 3:10 PM
jonymartin:

Can you explain some more about your environment, versions of the libraries, etc.?

M.
May 7, 2013 at 3:01 PM
I solved it! It was because of the version of the jquery! I was using the last one.
I changed to 1.8.3 and it works!
May 9, 2013 at 2:32 AM
Edited May 9, 2013 at 2:33 AM
Hi All,

I'm having a similar but strange version of this problem. I am using Sharepoint Foundation 2013 with SPServices 0.7.2 and JQuery 1.8.3-min. I have confirmed both are loading as mentioned in the General Instructions in the documentation.

My code is as follows:
var thisUserAccount = $().SPServices.SPGetCurrentUser({
    fieldName: 'Title',
    debug: true
});

alert('>'+thisUserAccount+'<'); // I've also tried putting the getuser function directly in the alert. Same results.
When I run this under my Site Owner account it works perfectly, returning the display name of my account. When I run it under a regular user account, it returns blank. I am confident the permissions to the jQuery and SPServices js files are fine as I have another page of the site using the cascading dropdowns which works perfectly for all users.

There is no debug info given by SPServices. I tested the alert($().SPServices.SPGetCurrentSite()); function and this works correctly for all users.

Are there any other reasons why SPGetCurrentUser might return blank for regular users? Thanks for any help you can give.
Coordinator
May 21, 2013 at 5:02 AM
No particular ideas on this one, Owen. Did you work it out?

M.
May 21, 2013 at 5:26 AM
Unfortunately, no. I went back to chaining context loads to grab the info I needed, which works but is obviously much slower.
Jun 19, 2013 at 3:10 PM
Hi owenbrady,

In the hope you might still be seeing this - have you tried accessing /_layouts/userdisp.aspx as a standard user? AFAIK, SPGetCurrentUser simply scrapes this page, so viewing the original as one of your standard users might reveal more, then simply accessing it by using SPServices? My initial suspicion says this sounds potentially like a permissions issue, but it is only a hunch...
Aug 9, 2013 at 6:54 PM
I am getting the same issue of not getting the user info using SPGetCurrentUser.() I have tried the following:
-- Quotes are fine
-- Able to access the userdisp.aspx as the user.
-- Using the following version of js files:
    jquery-1.9.0.js  , jquery.SPServices-0.7.2.min.js
-- Other functions that I have tried are working for example -- GetListItems using SPServices and $().SPServices.SPGetCurrentSite()
-- I put alert around the call and the second alert doesn't get called.

var thisSite = $().SPServices.SPGetCurrentSite();
alert(thisSite);
sUserEmail = $().SPServices.SPGetCurrentUser({ fieldName: 'EMail', debug: false });
alert(sUserEmail);

Any idea what could be the issue?
Coordinator
Aug 9, 2013 at 7:04 PM
Edited Aug 9, 2013 at 7:25 PM
The jQuery 1.9.x versions don't work with SPServices. There are known bugs in both 1.9.x versions, as noted on the home page.

M.
Aug 9, 2013 at 7:18 PM
Thanks for the quick reply...
I thought it worked with 1.9.0 version as per the comment ---
2013-01-29 - SPServices works just fine with jQuery 1.9.0, but NOT with the minified version

I tried with 1.8.3 and it worked fine...Thanks!!!
Coordinator
Aug 9, 2013 at 7:26 PM
Yes, SPServices ought to work with the unminified 1.9.0, but they are already up into the 1.10.x range, so you might as well go there if you haven't laid down jQuery already. I've tested up to 1.10.2, but not rigorously. It's hard to keep up with them.

M.
Apr 15, 2014 at 4:56 PM
I am experiencing the same issues bohrar. I've downgraded to jquery 1.8.3 and spservices 0.7.2, without any improvement. I tried mixing various versions of each with the same results. Calls work for site members, but non site members with contribute rights get nothing. Calls returns nothing. Any suggestions?

function GetUserData() {
var thisUserAccount = "";
var thisUserName    = "";
$(document).ready(function () {
    thisUserAccount = $().SPServices.SPGetCurrentUser({
        fieldName: 'Name',
        debug: false
    });        
    var tstarray = thisUserAccount;
    var myUserID = tstarray.slice(4);
    thisUserName = $().SPServices.SPGetCurrentUser({
        fieldName: 'Title',
        debug: false
    });