0

How can you get the current users id using javascript? Can you do this any JS file or does it need to be added at a certain point?

3 Answers 3

3

My favorite is to use the SPServices jQuery library, specifically the GetCurrentuser operation.

2

Since you are working on 2010 version you can use Client Object Model to get current user (and some other cool stuff).

Here is link to simple example:

Code:

<script type="text/javascript"> ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js"); var context = null; var web = null; var currentUser = null; function getWebUserData() { context = new SP.ClientContext.get_current(); web = context.get_web(); currentUser = web.get_currentUser(); currentUser.retrieve(); context.load(web); context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod)); } function onSuccessMethod(sender, args) { var userObject = web.get_currentUser(); alert('User name:' + userObject.get_title() + '\n Login Name:' + userObject.get_loginName()); } function onFailureMethod(sender, args) { alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); } </script> 
2

Just wanted to share: If you are looking for the ID of the current user only, there is no need to use any framework or API - SharePoint (at least in 2010, don't know in other versions) saves the ID in a global javascript variable named '_spUserId'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.