SharePoint Framework - Retrieve User Profile Information

User Profile service in SharePoint is useful to store information related to users. SharePoint out of box provides various user profile properties to denote user attributes (e.g. Id, First Name, Last Name, Manager, etc.). We can also create our own custom user profile properties. In this article, we will explore how to retrieve the user profile information. We will use ReactJS in this example.

Overview

User Profile service in SharePoint is useful to store information related to users. SharePoint out-of-box provides various user profile properties to denote user attributes (e.g. Id, First Name, Last Name, Manager, etc.). We can also create our own custom user profile properties.

In this article, we will explore how to retrieve the user profile information. We will use ReactJS in this example.

User Profile in SharePoint Online

Open https://[tenant]-admin.sharepoint.com.

From left navigation, click "user profiles".

User profile properties and user profiles can be managed from here.

Create SPFx Solution

Open the command prompt. Create a directory for SPFx solution.

md spfx-react-userprofile

Navigate to the above-created directory.

cd spfx-react-userprofile

Run Yeoman SharePoint Generator to create the solution.

yo @microsoft/sharepoint

Yeoman Generator will present you with the wizard by asking questions about the solution to be created.

Solution Name

Hit Enter to have the default name (spfx-react-userprofile in this case) or type in any other name for your solution.

Selected choice - Hit Enter

Target for component

Here, we can select the target environment where we are planning to deploy the client webpart, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).

Selected choice - SharePoint Online only (latest)

Place of files

We may choose to use the same folder or create a subfolder for our solution.

Selected choice - Same folder

Deployment option

Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.

Selected choice - N (install on each site explicitly)

Type of client-side component to create

We can choose to create client side webpart or an extension. Choose the webpart option.

Selected choice - WebPart

Web part name

Hit Enter to select the default name or type in any other name.

Selected choice - UserProfileViewer

Web part description

Hit Enter to select the default description or type in any other value.

Selected choice - Fetch user profile values with SPFx

Framework to use

Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout).

Selected choice - React

Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take a significant amount of time. Once the scaffolding process is completed, lock down the version of project dependencies by running the below command.

npm shrinkwrap

In the command prompt, type the below command to open the solution in the code editor of your choice.

code .

Define Model for User Profile

Let us start by defining the model for user profile properties.

Add a file IUserProfile.ts under “src\webparts\userProfileViewer\components\” folder with the below content.

export interface IUserProfile {

FirstName: string;

LastName: string;

Email: string;

Title: string;

WorkPhone: string;

DisplayName: string;

Department: string;

PictureURL: string;

UserProfileProperties: Array<any>;

}

Update IUserProfileViewerProps.ts under “src\webparts\userProfileViewer\components\” folder to include below properties.