SharePoint Framework is the new development model in which a lot of work has been going on since it went for General Availability on Feb 23, 2017. It is a page and web part model that provides full support for client-side SharePoint development, easy integration with SharePoint data, and support for open source tooling. With the SharePoint Framework, you can use modern web technologies and tools in your preferred development environment to build productive experiences and apps in SharePoint.
In a previous article, we saw how to set up the development environment for SharePoint Framework. We also saw the basic Hello World web part creation and how to retrieve and display list items using SharePoint Framework.
In this section, we will see another example of SPFx and PnP in action, here we will be using this combo to retrieve the user profile properties and display them in the web part. The major project files used in this solution have been zipped and uploaded along with this article. Feel free to download it.
Create the Web Part ProjectSpin up Node.js command prompt, using which we will be creating the Web part project structure. We can create the directory where we will be adding the solution, using the command given below.
md GetUserProfileProperties
Let’s move to the newly created working directory, using the command.
cd GetUserProfileProperties
We will then create the client Web part by running the Yeoman SharePoint Generator.
yo @microsoft/sharepoint
This will display the prompt which we will have to fill up so as to proceed with the project creation.
- What is your solution name? : Set it to ‘GetUserProfileProperties’.
On pressing enter, we will be asked to chose the working folder for the project.
- Where do you want to place your files? - Use current folder.
- What framework would you like to start with? - Select “No javaScript web framework” for the time being, as this is a sample Web part.
- What is your Webpart name? - We will specify it as ‘GetUserProfileProperties’ and press Enter.
- What is your Webpart description? - We will specify it as ‘Retrieve User Properties using SharePoint Framework’.

Run code to open the project in Visual Studio Code.
Now, we have to load PnP JS file which we will use within the project to create a list. We will be using npm to add the PnP JS file as shown below.
npm install sp-pnp-js --save
Retrieve User Profile data
In order to use PnP methods, we can refer the PnP file in the project as below.
import * as pnp from 'sp-pnp-js';
We will then make use of the below function to fetch the user profile properties of the user and display it within the web part. Th pnp.sp.profiles.myProperties.get() will return the current user’s profile properties which can be iterated to return the required information.
- private GetUserProperties(): void {
- sp.profiles.myProperties.get().then(function(result) {
- var userProperties = result.UserProfileProperties;
- var userPropertyValues = "";
- userProperties.forEach(function(property) {
- userPropertyValues += property.Key + " - " + property.Value + "<br/>";
- });
- document.getElementById("spUserProfileProperties").innerHTML = userPropertyValues;
- }).catch(function(error) {
- console.log("Error: " + error);
- });
- }
The entire TS file content is shown below. The this.GetUserProperties() in the render method will call the function that gets the User Profile properties of the user. It will be then displayed within the div element declared in the render method.
- import * as pnp from 'sp-pnp-js';
- import {
- Version
- } from '@microsoft/sp-core-library';
- import {
- BaseClientSideWebPart,
- IPropertyPaneConfiguration,
- PropertyPaneTextField
- } from '@microsoft/sp-webpart-base';
- import {
- escape
- } from '@microsoft/sp-lodash-subset';
- import styles from './GetUserProfileProperties.module.scss';
- import * as strings from 'getUserProfilePropertiesStrings';
- import {
- IGetUserProfilePropertiesWebPartProps
- } from './IGetUserProfilePropertiesWebPartProps';
- export default class GetUserProfilePropertiesWebPart extends BaseClientSideWebPart < IGetUserProfilePropertiesWebPartProps > {
- private GetUserProperties(): void {
- sp.profiles.myProperties.get().then(function(result) {
- var userProperties = result.UserProfileProperties;
- var userPropertyValues = "";
- forEach(function(property) {
- userPropertyValues += property.Key + " - " + property.Value + "<br/>";
- });
- getElementById("spUserProfileProperties").innerHTML = userPropertyValues;
- }).catch(function(error) {
- log("Error: " + error);
- });
- }
- public render(): void {
- domElement.innerHTML = `
- <div class="${styles.helloWorld}">
- <div class="${styles.container}">
- <div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
- <div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPusstrong ms-u-lgPusstrong">
- <span class="ms-font-xl ms-fontColor-white" style="font-size:28px">Welcome to SharePoint Framework Development using PnP JS Library</span>
- <p class="ms-font-l ms-fontColor-white" style="text-align: left">Demo : Retrieve User Profile Properties</p>
- </div>
- </div>
- <div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
- <div style="background-color:Black;color:white;text-align: center;font-weight: bold;font-size:18px;">User Profile Details</div>
- <br>
- <div id="spUserProfileProperties" />
- </div>
- </div>
- </div>`;
- GetUserProperties();
- }
- protected get dataVersion(): Version {
- return Version.parse('1.0');
- }
- protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
- return {
- pages: [{
- header: {
- description: strings.PropertyPaneDescription
- },
- groups: [{
- groupName: strings.BasicGroupName,
- groupFields: [
- PropertyPaneTextField('description', {
- label: strings.DescriptionFieldLabel
- })
- ]
- }]
- }]
- };
- }
- }
Now, let’s test the Web part in SharePoint Workbench available in SharePoint Online. Once we have logged into SharePoint Online, we can invoke the workbench by appending the text ‘_layouts/15/workbench.aspx’ to SharePoint Online URL. Add the web part to the page by selecting GetUserProfile icon.
This will add the web part to the page and it will fetch the user profile details of the user and display it.
The major project files used in this solution have been zipped and uploaded along with this article.
Summary
Thus, we saw how to retrieve user profile properties from SharePoint Online using SharePoint Framework and PnP JS. We will see more of SPFx in action in the upcoming articles.

Sheen WanPosted Sep 11, 2021, 6:10 AM
Good article thanks for sharing it keep it up
Guy KaytonPosted Jul 19, 2019, 12:42 PM
Please help with this incomplete solution!
Guy KaytonPosted Jul 19, 2019, 12:00 PM
This is exactly what I need but it is not working. Need help. There is an import for getUserProfilePropertiesStrings but this is not found and I don't see a file in your solution. I build as described but all I get is Welcome to SharePoint!Customize SharePoint experiences using Web Parts. getuserprofileproperties Learn more
Navneet KumarPosted May 13, 2019, 7:09 AM
Hi, Can you please let me know how can i use this spfx in angular 4+ and get data from the list and library.
sravan kumarPosted May 2, 2018, 12:39 AM
If we add this client side webpart in site pages.. Then it is not working. Could you help me ??
Rohit ChoudharyPosted Apr 20, 2018, 7:31 AM
Will it pull custom properties also??
Praveen KanamarlapudiPosted Mar 26, 2018, 2:12 AM
Good article, can you please provide details for adding a person field value into list using spfx.