How do I get the user Id from user information list using the user name with help of javascript?

8.1k Views Asked by At

I have a condition in which I add people to user information list and then would like to add them to a custom list in my site. Using sharepoint search api I was able to get the Preferred Name and am able to add the person to the user information list,but now that I am not getting an ID, how can I add the person?

Or Is there any chance of getting the user Id from user name? Thanks in advance

1

There are 1 best solutions below

1
On

We can use REST API to get the list items by user name. The code below for your reference:

<script type="text/javascript" src="//code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
$(function() {
    var username="test2";
    // begin work to call across network
    var requestUri = _spPageContextInfo.webAbsoluteUrl+"/_api/web/SiteUserInfoList/items?$select=Id&$filter=Title eq '"+username+"'";
    //execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        success: function (data) {
            if(data.d.results.length>0){
                var userID=data.d.results[0].Id;
                alert(userID);
            }

        },
        error: function () {
            //alert("Failed to get details");
        }
    });
});
</script>