SharePoint 2016 - How To Get SPUser By User ID

In this article we will see how to query data for the SharePoint Users based on User ID.

While working with a whole range of applications using SharePoint Platform & Services I have come across lot of issues, especially while dealing with SharePoint User Information.

In most scenarios you will need this to query data for the users based on the User ID from within the code itself. I have add a little User Interface in this article to make sure you get the idea on how this approach will work and what properties will be exposed as a result.

Here is the process flow that depicts the flow of information between Client Request & Server Response:

SharePoint

To start with the demo I have added some of HTML elements to prepare the UI with a textbox to enter User ID as shown below:

 

SharePoint

 

In the below screen shot you can see the simple HTML markup for the user interface.

We have a textbox where users can enter user id of the SharePoint User

 

SharePoint

 

In order to display the results I have added an HTML table as container. The purpose is to prepare HTML on the fly and paste it at runtime into this container.

 

SharePoint

 

In Step 1 we have bound the blur event of the textbox to a function that will execute the query against the User Data based on the User ID

 

SharePoint

 

In Step 2 we call another helper function “getUserById” by passing user id to it. This function call returns a jQuery promise which can be further evaluated in upcoming steps.

In Step 3 we will check if the JQuery call has been completed or not by using JQuery “when” construct.

In Step 4 we will call another helper function “renderUser” once the JQuery call has been completed in Step 3. The “renderUser” function is responsible to render User Information into the container.

 

SharePoint

 

In Step 5 we call “_api/Web” REST API endpoint using its function “getUserById”; during this call we will specify “json” as datatype to ensure that we will get results in “json” format.

 

SharePoint

 

In Step 6 we are rendering the details of the user in the container.

 

SharePoint

 

And here is the final output of the operation performed.

So we can see Title, Login Name, Email returned back for a specific User Id as shown below.

 

SharePoint

 

That is all for this demo.

Hope you find it helpful.