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:

0

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-

1

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

2

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.

3

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

4

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.

5

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

6

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

7

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-

8

That is all for this demo.

Hope you find it helpful.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s