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 Project
Spin 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’.
Edit the web part
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);
- });
- }
TS File content to retrieve User Profile Data
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
- })
- ]
- }]
- }]
- };
- }
- }
Test the Web part in SharePoint Online
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.