$().SPServices.SPGetCurrentUser isn't returning ID

May 23, 2014 at 5:39 AM
I'm trying to get the ID of the current user but it keeps returning undefined, whereas the other properties work fine so I don't think it's a jquery issue.

This is what I have:
//GET CURRENT USER INFORMATION
        var currentUser = $().SPServices.SPGetCurrentUser({
                              webURL: "",   
                              fieldNames: ["ID","EMail","UserName","Title","Picture"],
                              debug: false
                            });
May 23, 2014 at 12:06 PM
The "ID" field isn't available using that method (see https://spservices.codeplex.com/wikipage?title=$().SPServices.SPGetCurrentUser). If you want the user's ID, you could use "$().SPServices.SPGetCurrentUser()" to filter a GetListItems query on the User Information List such as.....
var lngUserID;
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "User Information List",
    CAMLViewFields: "<ViewFields Properties='True' />",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='Name' /><Value Type='Text'>" + $().SPServices.SPGetCurrentUser() + "</Value></Eq></Where></Query>",
    CAMLQueryOptions: "<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>",
    completefunc: function(xData, Status) {
        lngUserID = $(xData.responseXML).SPFilterNode("z:row").first().attr("ows_ID");
    }
});
Geoff
May 23, 2014 at 1:01 PM
Geoff,
That's a good tip. I did not know we could query that table. I'll have to try it.




--
Paul T.

-- Sent from Mobile

May 23, 2014 at 1:31 PM
Edited May 23, 2014 at 1:31 PM
I looked at the documentation and it says it's able to return ID:
fieldNames
Added in v0.7.2 to allow requesting multiple column values. The column names can be passed in as an array, such as ["ID", "Last Name"]
They even have an example:
var thisUserID = $().SPServices.SPGetCurrentUser({
    fieldName: "ID",
    debug: false
});
That's why I'm a little confused. I'll try your method but the documentation should be updated if it's not possible.
Coordinator
May 23, 2014 at 1:57 PM
Yes, ID should work. "They" is me. ;+)

What version of SharePoint are you on?

M.
May 23, 2014 at 2:48 PM
Ahh, my bad....I was only looking at the table of available fields on the documentation page; I missed the note above it. That may have been able to save me some work in the past! :P

Geoff
May 23, 2014 at 3:06 PM
Edited May 23, 2014 at 3:16 PM
So, my curiosity kicked in and I decided to do a little more learning on how Marc was getting the ID off the "userdisp.aspx" page and saw that his code calls a page variable that SP sets up, "_spUserId." I also read a post stating that "_spUserId is deprecated in SP2013." SimK, Are you using SP2013? The post I read recommends calling "_spPageContextInfo.userId" instead. I believe _spPageContextInfo is only available in SP2010 and later versions.

Marc, I don't know if you're already on this. I'm still running SPServices 0.7.1a so it may already be updated in a later release.

Geoff
Coordinator
May 23, 2014 at 3:12 PM
Yes, it's been working for a while, supposedly regardless what version. It'll be helpful to know what version SimK is running so I can fix it if there's a bug.

@Geoff - You should really upgrade!

M.
May 23, 2014 at 9:45 PM
Hey Marc,

I'm running Foundation SP 2010. I'm also using one of the 2014 versions of SPServices. Whatever is on cdnjs.
Coordinator
Jun 2, 2014 at 1:53 PM
Edited Jun 2, 2014 at 1:53 PM
SimK:

I think I may have a bug. If you request the ID singly, like so:
var currentUser = $().SPServices.SPGetCurrentUser({
  fieldName: "ID"
});
I think it will work fine. However, using fieldNames, it doesn't look like I do the appropriate check for undefined.

I'll add this to the Issue Tracker. In the meantime, you can just use the JavaScript variable _spPageContextInfo.userId.

M.
Coordinator
Jun 2, 2014 at 1:54 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Oct 16, 2014 at 1:46 PM
I've just uploaded a new alpha (2014.02ALPHA5) with a fix for this issue and a couple other things. If you could try it out and let me know if it works for you, that would be very helpful.
https://spservices.codeplex.com/releases/view/119578

M.
Oct 16, 2014 at 9:47 PM
sympmarc wrote:
I've just uploaded a new alpha (2014.02ALPHA5) with a fix for this issue and a couple other things. If you could try it out and let me know if it works for you, that would be very helpful.
https://spservices.codeplex.com/releases/view/119578

M.
Hi Mark,

Just gave it a shot, seems like it working perfectly now. Thanks!
Coordinator
Oct 17, 2014 at 1:15 AM
Awesome!

Keep in mind that it's an alpha, but it should be stable.

M.