Tuesday, July 29, 2008

Fetching Logged-in User ID from Javascript

Hi All,

There is an interesting JavaScript that i found while using the javascript code in webpart or any aspx page that you are developing for moss or even in master page or anywhere when you require to write a code.

This variable _spUserId is useful to know the ID of the logged in user in javascript.

Just try to login to your site and then go to address bar and then write javascript
javascript:alert(_spUserId); and see what it gives you and try using different IDs.

From there only you will get an idea that it is returning the IDs of the logged in user.

That's it.

8 comments:

Anonymous said...

Cool.. It works thanks

Anonymous said...

This alert returns '1'. What does that mean?

SharePoint Kings said...

its id of logged in user

kalyan said...

how wud you use the same inside the script??? I tried all the ways without any luk.

SharePoint Kings said...

kalyan, can explain how you are trying?because we are getting from the above method in moss 2007

kalyan said...

Thnx for the quick response,
below are the different ways that I have tried to make use of this variable.

< script lang="javascript" >
alert(_spUserId)

or

var userID = _spUserId;
alert(userID);
or

var userID = javascript:_spUserId;
alert(userID);
< /script >

none of these helped me in getting the ID of the current logged in user except typing in place of URL with "javascript:alert(_spUserId)" which we cant make use of that code in actual scripting.

Please let me know, if I have missed something to use.

SharePoint Kings said...

kalyan,

it looks like when your javascript is fired at that time it might be possible that ID was not available so try to put your javascript at the end if that was not working try to use

_spbodyonloadfunctionnames.push(yourfunc);

and in try
function yourfunc()
{alert(_spUserId);}

try this it should work if not then it might be something special for your case :(

Phoebe said...

How about user name?




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com