Click here to Skip to main content
13,767,632 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 18 Jul 2014
Licenced CPOL

SharePoint REST API: Get User Id by User Name

Rate this:
Please Sign up or sign in to vote.
The below tip will help developers retrieving user id by account name using SharePoint REST APIs.


This is not an article, but a small tip. In certain cases, it is required to fetch UserId of particular site users in SharePoint. This tip may be helpful in getting UserId for site user by passing login name. SharePoint provides OOB REST APIs to get site users, however, with login name format, which is dependent upon SharePoint environment, it becomes little tricky.

The REST endpoint to get site users is : http://<site url>/_api/web/siteusers(@v)?@v='<pass login name>'

Using the Code

The below code can be included on your page to fetch the UserId of a given Site User.

// Include JQuery reference
// ... script continues ...

        /// username should be passed as 'domain\username'
        function GetUserId(userName) {
            /// change this prefix according to the environment. 
            /// In below sample, windows authentication is considered.
            var prefix = "i:0#.w|";
            /// get the site url
            var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
            /// add prefix, this needs to be changed based on scenario
            var accountName = prefix + userName;

            /// make an ajax call to get the site user
                url: siteUrl + "/_api/web/siteusers(@v)?@v='" + 
                    encodeURIComponent(accountName) + "'",
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                    ///popup user id received from site users.
                    alert("Received UserId" + data.d.Id);
                error: function (data) {
  • For SharePoint Online or on-premise forms authentication, use the below login name format:
    i: 0#.f|membership|
  • For SharePoint 2013 on-premise Windows authentication, use the below login name format:
    i: 0#.w|domain\user
  • For SharePoint 2013 on-premise SAML based authentication, use the below login name format:
    ?i: 05:t|adfs with roles|

For examples of login names and further information, refer to 'table 1 Login name formats' in MSDN link:

Points of Interest

Without appending the login name format prefix, SharePoint returns error HTTP 500, hence it is important to identify suitable login format according to the environment and set as prefix on login name.

References: User, Groups and Roles, REST API reference and example:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.181116.1 | Last Updated 22 Jul 2014
Article Copyright 2014 by Chintankumar Panchasara
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid