SharePoint 2016: How To Get SPUser By User ID

While working with a whole range of applications using SharePoint Platform & Services I have come across lot of issues specially while dealing with SharePoint User Information let it by user names entered by application users on the UI or by querying user information by User ID.

In most scenarios you will need this to query data for the users based on User ID from within 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 all properties will be exposed as results.

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

https://howtodowithsharepoint.files.wordpress.com/2018/01/01.jpg?w=800

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

https://howtodowithsharepoint.files.wordpress.com/2018/01/11.png?w=800

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

https://howtodowithsharepoint.files.wordpress.com/2018/01/21.png?w=800

In order to display the results I have add a HTML table as container. Purpose is to prepare HTML on the fly and paste it at runtime into this container.

https://howtodowithsharepoint.files.wordpress.com/2018/01/31.png?w=800

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

https://howtodowithsharepoint.files.wordpress.com/2018/01/41.png?w=800

In Step 2 we call another helper function “getUserById” by passing user id to it. This function call return 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.

https://howtodowithsharepoint.files.wordpress.com/2018/01/51.png?w=800

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

https://howtodowithsharepoint.files.wordpress.com/2018/01/61.png?w=800

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

https://howtodowithsharepoint.files.wordpress.com/2018/01/71.png?w=800

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-

https://howtodowithsharepoint.files.wordpress.com/2018/01/81.png?w=800

That is all for this demo.