_spUserId is undefined

Jul 28, 2011 at 8:36 PM

Hi,

In sharepoint 2007 I try to retrieve the user ID.

I'm using the following js files:  jquery-1.5.2.min.js  and  jquery.SPServices-0.6.2.min.js

When I execute

 var loginID = $().SPServices.SPGetCurrentUser({
   fieldName: "ID"
   });

It's returning an error: _spUserId is undefined

Thanks for your help

Pat

 

 

Coordinator
Jul 28, 2011 at 11:42 PM

What type of page is it where you are making the call? What version of SharePoint 2007? Is loginID being set to the string "_spUserId is undefined", or are you seeing it in some other way?

M.

Jul 29, 2011 at 12:57 AM

It's an aspx page, where I use javascript to call the service. Sharepoint is MOSS 2007.  I have that error when I debug javascript.

Thanks,

Pat

 

 

Coordinator
Jul 29, 2011 at 1:12 AM
Edited Jul 29, 2011 at 1:14 AM

I just tested this:

 $(document).ready(function() {
  var loginID = $().SPServices.SPGetCurrentUser({
   fieldName: "ID"
  });
  alert(loginID);
 });

in WSS and it alerted my ID.

Are you using $(document).ready()? It could be that _spUserId hasn't been defined when you call SPGetCurrentUser.

M.

Jul 29, 2011 at 2:34 AM

Hi Marc,

It was that. I was calling it before $(document).ready(function()

Thanks again for your help. It's working now.

Pat

Coordinator
Jul 29, 2011 at 3:59 AM

Great. Glad it's working.

M.

Aug 1, 2012 at 8:36 AM

I keep getting

Uncaught ReferenceError: _spUserId is not defined

I'm definitely calling the method when jQuery is ready I have even tried it on click of a button.

When is that variable assigned? And what assigns it?

Aug 1, 2012 at 2:06 PM
If I remember correctly, this variable is set by one of the SP javascript files. You need to make sure you are trying to access it after those js files are loaded. What I found recently is that if you are running in anonymous mode, this variable is not set... could that be the issue?
Try the following to test it without SPServices:

alert("ID of current user: " + window._spUserId);


Mark,
Does your code account for the possibility of that variable not being set? (I had to work around this issue during the #EUSPGames challenge, if I wanted to have anonymous mode turned on).

_________
Paul T



On Wed, Aug 1, 2012 at 3:36 AM, denzo <notifications@codeplex.com> wrote:

From: denzo

I keep getting

Uncaught ReferenceError: _spUserId is not defined

I'm definitely calling the method when jQuery is ready I have even tried it on click of a button.

When is that variable assigned? And what assigns it?

Read the full discussion online.

To add a post to this discussion, reply to this email (SPServices@discussions.codeplex.com)

To start a new discussion for this project, email SPServices@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
Aug 1, 2012 at 2:15 PM

I just checked and I'm assuming that the value is always available. I'll add a check for it being undefined in the next alpha. I'm assuming it was undefined, not null?

M.

Coordinator
Aug 1, 2012 at 2:20 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Aug 1, 2012 at 2:21 PM
If I recall, it was undefined... I think I used a try{} block to test for it.

_________
Paul T


Coordinator
Aug 1, 2012 at 2:22 PM

In what context were you seeing the error? I'm grabbing the value of _spUserId in SPGetCurrentUser if the ID is requested, just to save the expense of loading the userdisp.aspx page, as I do to get other attributes of the current user. However, if you're calling SPGetCurrentUser or another function that calls it, then there are probably other issues when _spUserId isn't defined.

M.

Aug 1, 2012 at 2:25 PM
Mark,
In my case, I wanted to get the ID of the current user in order to save some settings in a list... I was not even trying to use SPServices when I found this issue because I knew that SP already stored the current user's ID in that global variable.
_________
Paul T


Coordinator
Aug 1, 2012 at 2:34 PM

Got it; thanks.

In any case, I'll add a check, but there will probably be some further unpleasantness down the road when I try to grab userdisp.aspx.

M.

Aug 2, 2012 at 12:46 AM

I have noticed that _spUserId is assigned on dynamic pages generated by SP (default.aspx). I am using an HTML page that I have created from scratch, therefore that variable is undefined (not null).

I noticed in the code that you are checking for ID as a field name and not making a request to userdisp.aspx (which you have mentioned in one of your replies). Would be awesome if it checked for undefined and made a request if it was undefined (which I think you meant to suggest in one of your replies also).

The workaround I have done goes like so: from SPGetCurrectUser I get the userLoginName and then use that to make another call with GetUserInfo providing the userLoginName.

2 calls instead of 1 is not ideal. Therefore, I'm looking forward for the fix.