I need to retrieve current user id from login name.

How can that be done? I have used the following code:

var userid=Function??("domain\name");

Assuming you are not looking for the current User...I imagine it would be something like this, although I have not tested this nor have I done it before.

    var clientContext = SP.ClientContext.get_current();
    var website = clientContext.get_web();
    currentUser = website.ensureUser("domain\name");
    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);

    function onRequestSucceeded() {
       var userid =  currentUser.get_id();

    function onRequestFailed(sender, args) {
        //error handling
        alert('Error: ' + args.get_message());

Although it is important to note that ensure user will add the users to the given web if they don't already exist.

| improve this answer | |
  • all correct except the currentUser.UserID should be currentUser.get_id() – svnm Mar 3 '15 at 6:17

There's no need to make a request to the server when the userid is already loaded in browser.

You can use:


available in SharePoint 2013 and Online (wave 16).

| improve this answer | |
  • 1
    This one line code is fine but user does not need current login user id – Zakir HC Jul 10 '17 at 6:14

To get the userId of a user from the account/login name can be obtained by:

        function GetUserIdFromUserName(userName) {
        /// according to the environment.
        var prefix = "i:0#.w|";
        var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
        var accountName = prefix + userName;
            url: siteUrl + "/_api/web/siteusers(@v)?@v='" + 
                encodeURIComponent(accountName) + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                alert("Received UserId " + data.d.Id);
            error: function (data) {
| improve this answer | |

See for an example on how to get the current user in JavaScript.

Once you have the user, you can use .get_id() to get the record ID for the user.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy