How to use fluent UI react persona control in SPFx?

Published Jun 10 2021 05:56 AM 1,155 Views
Senior Member

Introduction

 

A persona is a visual representation of a person across products, typically showcasing the image that person has chosen to upload themselves. This control includes an individual's avatar (an uploaded image or a composition of the person’s initials on a background color), their name or identification, and so on. for more details refer to this.

 

Scenario 

 

We will create an SPFx web part in the way to fetch users from any specific group and render these users using a persona. so let's see step-by-step implementation.

 

In the end, our output will be like this,

 

Output.png

 

Implementation  

 

Open a command prompt
Move to the path where you want to create a project
Create a project directory using:

 

md spfx-persona

 

Move to the above-created directory using:
 
cd spfx-persona
 
Now execute the below command to create an SPFx solution:
 
yo @microsoft/sharepoint 
 
It will ask some questions, as shown below,
 
Project Structure.png

 

Now we will install pnpjs as shown below:

 

npm install @pnp/sp --save

 

After a successful installation, we can open a project in any source code tool. Here, I am using the VS code, so I will execute the command:

 

code .
 
1. Move to the src/webparts/spfxpersona/components/ISpfxpersonaProps.ts and create a context property as below,
 
import { WebPartContext } from "@microsoft/sp-webpart-base";

export interface ISpfxpersonaProps {
  description: string;
  context: WebPartContext
}
 
2. Now move to the SpfxpersonaWebPart.ts file and here we initialize an SP context and pass it in the property.
 
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
  IPropertyPaneConfiguration,
  PropertyPaneTextField
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';

import * as strings from 'SpfxpersonaWebPartStrings';
import Spfxpersona from './components/Spfxpersona';
import { ISpfxpersonaProps } from './components/ISpfxpersonaProps';
import { sp } from "@pnp/sp/presets/all";

export interface ISpfxpersonaWebPartProps {
  description: string;
}

export default class SpfxpersonaWebPart extends BaseClientSideWebPart<ISpfxpersonaWebPartProps> {

  public onInit(): Promise<void> {
    return super.onInit().then(_ => {
      sp.setup({
        spfxContext: this.context
      });
    });
  }

  public render(): void {
    const element: React.ReactElement<ISpfxpersonaProps> = React.createElement(
      Spfxpersona,
      {
        description: this.properties.description,
        context: this.context,
      }
    );

    ReactDom.render(element, this.domElement);
  }

  protected onDispose(): void {
    ReactDom.unmountComponentAtNode(this.domElement);
  }

  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
                })
              ]
            }
          ]
        }
      ]
    };
  }
}
 
3. Create a common component means folder called RenderProfilePicture inside src/webparts/spfxpersona/Common/Components. And inside this folder create a file RenderProfilePicture.tsx .
 
At here we will pass a property as an argument and render it and in the main component, we will call API to get items so they will be set through the property. 
 
import * as React from 'react';
import { Persona, PersonaSize } from 'office-ui-fabric-react/lib/Persona';
interface IProfilePicProps {
    loginName: string;
    displayName: string;
    getUserProfileUrl?: () => Promise<string>;
}

export function RenderProfilePicture(props: IProfilePicProps) {

    const [profileUrl, setProfileUrl] = React.useState<string>();
    let { displayName, getUserProfileUrl } = props;

    React.useEffect(() => {
        getUserProfileUrl().then(url => {
            setProfileUrl(url);
        });
    }, [props])

    return (
        <div>
            <Persona
                imageUrl={profileUrl}
                text={displayName}
                size={PersonaSize.size32}
                imageAlt={displayName}
                styles={{ primaryText: { fontSize: '14px' }, root: { margin: '10px' } }}
            />
        </div>);
}
 
4. Now move to the Spfxpersona.tsx our main component. here we will call APIs and render the child component here.
 
Create a state interface and initialize a user state.
At here we are getting the user from a particular group ID so first call API to get users from the group.
And then we will get user profile properties to get users' profile picture so after calling the get users method we will call API to get user profile properties.
And then render the component.
 
import * as React from 'react';
import styles from './Spfxpersona.module.scss';
import { ISpfxpersonaProps } from './ISpfxpersonaProps';
import { escape } from '@microsoft/sp-lodash-subset';
import { sp } from "@pnp/sp/presets/all";
import { RenderProfilePicture } from '../Common/Components/RenderProfilePicture/RenderProfilePicture'

export interface ISpfxpersonaWebPartState {
  users: any[]
}
export default class Spfxpersona extends React.Component<ISpfxpersonaProps, ISpfxpersonaWebPartState> {

  constructor(props: ISpfxpersonaProps) {
    super(props);
    this.state = {
      users: []
    }
  }

  private async getUserProfileUrl(loginName: string) {
    const userPictureUrl = await sp.profiles.getUserProfilePropertyFor(loginName, 'PictureURL');
    return userPictureUrl;
  }

  private async getSiteUsers() {
    const grpUsers = await sp.web.siteGroups.getById(3).users();
    this.setState({ users: grpUsers })
  }

  public componentDidMount() {
    this.getSiteUsers()
  }

  public render(): React.ReactElement<ISpfxpersonaProps> {
    return (
      <div className={styles.spfxpersona}>
        <span>USERS WITH PERSONA CARD</span>
        {this.state.users.map(m =>
          <RenderProfilePicture
            loginName={m.LoginName}
            displayName={m.Title}
            getUserProfileUrl={() => this.getUserProfileUrl(m.LoginName)}  ></RenderProfilePicture>
        )}
      </div>
    );
  }
}
 
Now serve the application using the below command,
 
gulp serve
 
Now test the webpart in SharePoint-SiteURL + /_layouts/15/workbench.aspx.
 

Output

 

Output.png

 

 

Find the full source code here.

 

 

Summary

 

In this article, we have seen the step-by-step implementation of how to use a persona card to show the users' profile picture.

I hope this helps, if this helps you then share it with others.

Sharing is caring!

%3CLINGO-SUB%20id%3D%22lingo-sub-2415229%22%20slang%3D%22en-US%22%3EHow%20to%20use%20fluent%20UI%20react%20persona%20control%20in%20SPFx%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2415229%22%20slang%3D%22en-US%22%3E%3CH2%20id%3D%22toc-hId--440178667%22%20id%3D%22toc-hId--442000781%22%3EIntroduction%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20persona%20is%20a%20visual%20representation%20of%20a%20person%20across%20products%2C%20typically%20showcasing%20the%20image%20that%20person%20has%20chosen%20to%20upload%20themselves.%20This%20control%20includes%20an%20individual's%20avatar%20(an%20uploaded%20image%20or%20a%20composition%20of%20the%20person%E2%80%99s%20initials%20on%20a%20background%20color)%2C%20their%20name%20or%20identification%2C%20and%20so%20on.%20for%20more%20details%20refer%20to%20%3CA%20href%3D%22https%3A%2F%2Fdeveloper.microsoft.com%2Fen-us%2Ffluentui%23%2Fcontrols%2Fweb%2Fpersona%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ethis%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2047334166%22%20id%3D%22toc-hId-2045512052%22%3EScenario%26nbsp%3B%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20will%20create%20an%20SPFx%20web%20part%20in%20the%20way%20to%20fetch%20users%20from%20any%20specific%20group%20and%20render%20these%20users%20using%20a%20persona.%20so%20let's%20see%20step-by-step%20implementation.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20end%2C%20our%20output%20will%20be%20like%20this%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Output.png%22%20style%3D%22width%3A%20315px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F286246i238B2956F1E85171%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Output.png%22%20alt%3D%22Output.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-239879703%22%20id%3D%22toc-hId-238057589%22%3EImplementation%26nbsp%3B%26nbsp%3B%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOpen%20a%20command%20prompt%3CBR%20%2F%3EMove%20to%20the%20path%20where%20you%20want%20to%20create%20a%20project%3CBR%20%2F%3ECreate%20a%20project%20directory%20using%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Emd%20spfx-persona%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20id%3D%22pastingspan1%22%3EMove%20to%20the%20above-created%20directory%20using%3A%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CPRE%3Ecd%20spfx-persona%3C%2FPRE%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%20id%3D%22pastingspan1%22%3ENow%20execute%20the%20below%20command%20to%20create%20an%20SPFx%20solution%3A%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CPRE%3Eyo%20%40microsoft%2Fsharepoint%20%3C%2FPRE%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%20id%3D%22pastingspan1%22%3EIt%20will%20ask%20some%20questions%2C%20as%20shown%20below%2C%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Project%20Structure.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F286247iB48EBA3A11505543%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Project%20Structure.png%22%20alt%3D%22Project%20Structure.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CP%3ENow%20we%20will%20install%20%3CSTRONG%3Epnpjs%3C%2FSTRONG%3E%20as%20shown%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Enpm%20install%20%40pnp%2Fsp%20--save%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20a%20successful%20installation%2C%20we%20can%20open%20a%20project%20in%20any%20source%20code%20tool.%20Here%2C%20I%20am%20using%20the%20VS%20code%2C%20so%20I%20will%20execute%20the%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Ecode%20.%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E1.%20Move%20to%20the%26nbsp%3B%3CEM%3Esrc%2Fwebparts%2Fspfxpersona%2Fcomponents%2F%3CSTRONG%3EISpfxpersonaProps.ts%26nbsp%3B%3C%2FSTRONG%3E%3C%2FEM%3Eand%20create%20a%20context%20property%20as%20below%2C%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eimport%20%7B%20WebPartContext%20%7D%20from%20%22%40microsoft%2Fsp-webpart-base%22%3B%0A%0Aexport%20interface%20ISpfxpersonaProps%20%7B%0A%20%20description%3A%20string%3B%0A%20%20context%3A%20WebPartContext%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E2.%20Now%20move%20to%20the%26nbsp%3B%3CSTRONG%3ESpfxpersonaWebPart.ts%26nbsp%3B%3C%2FSTRONG%3Efile%20and%20here%20we%20initialize%20an%20SP%20context%20and%20pass%20it%20in%20the%20property.%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eimport%20*%20as%20React%20from%20'react'%3B%0Aimport%20*%20as%20ReactDom%20from%20'react-dom'%3B%0Aimport%20%7B%20Version%20%7D%20from%20'%40microsoft%2Fsp-core-library'%3B%0Aimport%20%7B%0A%20%20IPropertyPaneConfiguration%2C%0A%20%20PropertyPaneTextField%0A%7D%20from%20'%40microsoft%2Fsp-property-pane'%3B%0Aimport%20%7B%20BaseClientSideWebPart%20%7D%20from%20'%40microsoft%2Fsp-webpart-base'%3B%0A%0Aimport%20*%20as%20strings%20from%20'SpfxpersonaWebPartStrings'%3B%0Aimport%20Spfxpersona%20from%20'.%2Fcomponents%2FSpfxpersona'%3B%0Aimport%20%7B%20ISpfxpersonaProps%20%7D%20from%20'.%2Fcomponents%2FISpfxpersonaProps'%3B%0Aimport%20%7B%20sp%20%7D%20from%20%22%40pnp%2Fsp%2Fpresets%2Fall%22%3B%0A%0Aexport%20interface%20ISpfxpersonaWebPartProps%20%7B%0A%20%20description%3A%20string%3B%0A%7D%0A%0Aexport%20default%20class%20SpfxpersonaWebPart%20extends%20BaseClientSideWebPart%3CISPFXPERSONAWEBPARTPROPS%3E%20%7B%0A%0A%20%20public%20onInit()%3A%20Promise%3CVOID%3E%20%7B%0A%20%20%20%20return%20super.onInit().then(_%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20sp.setup(%7B%0A%20%20%20%20%20%20%20%20spfxContext%3A%20this.context%0A%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D)%3B%0A%20%20%7D%0A%0A%20%20public%20render()%3A%20void%20%7B%0A%20%20%20%20const%20element%3A%20React.ReactElement%3CISPFXPERSONAPROPS%3E%20%3D%20React.createElement(%0A%20%20%20%20%20%20Spfxpersona%2C%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20description%3A%20this.properties.description%2C%0A%20%20%20%20%20%20%20%20context%3A%20this.context%2C%0A%20%20%20%20%20%20%7D%0A%20%20%20%20)%3B%0A%0A%20%20%20%20ReactDom.render(element%2C%20this.domElement)%3B%0A%20%20%7D%0A%0A%20%20protected%20onDispose()%3A%20void%20%7B%0A%20%20%20%20ReactDom.unmountComponentAtNode(this.domElement)%3B%0A%20%20%7D%0A%0A%20%20protected%20get%20dataVersion()%3A%20Version%20%7B%0A%20%20%20%20return%20Version.parse('1.0')%3B%0A%20%20%7D%0A%0A%20%20protected%20getPropertyPaneConfiguration()%3A%20IPropertyPaneConfiguration%20%7B%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20pages%3A%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20header%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20description%3A%20strings.PropertyPaneDescription%0A%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20groups%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20groupName%3A%20strings.BasicGroupName%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20groupFields%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20PropertyPaneTextField('description'%2C%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3A%20strings.DescriptionFieldLabel%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%3B%0A%20%20%7D%0A%7D%0A%3C%2FISPFXPERSONAPROPS%3E%3C%2FVOID%3E%3C%2FISPFXPERSONAWEBPARTPROPS%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E3.%20Create%20a%20common%20component%20means%20folder%20called%26nbsp%3B%3CSTRONG%3ERenderProfilePicture%3C%2FSTRONG%3E%20inside%26nbsp%3B%3CEM%3Esrc%2Fwebparts%2Fspfxpersona%2FCommon%2FComponents.%26nbsp%3B%3C%2FEM%3EAnd%20inside%20this%20folder%20create%20a%20file%26nbsp%3B%3CSTRONG%3ERenderProfilePicture.tsx%3C%2FSTRONG%3E%26nbsp%3B%3CSTRONG%3E.%3C%2FSTRONG%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3EAt%20here%20we%20will%20pass%20a%20property%20as%20an%20argument%20and%20render%20it%20and%20in%20the%20main%20component%2C%20we%20will%20call%20API%20to%20get%20items%20so%20they%20will%20be%20set%20through%20the%20property.%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eimport%20*%20as%20React%20from%20'react'%3B%0Aimport%20%7B%20Persona%2C%20PersonaSize%20%7D%20from%20'office-ui-fabric-react%2Flib%2FPersona'%3B%0Ainterface%20IProfilePicProps%20%7B%0A%20%20%20%20loginName%3A%20string%3B%0A%20%20%20%20displayName%3A%20string%3B%0A%20%20%20%20getUserProfileUrl%3F%3A%20()%20%3D%26gt%3B%20Promise%3CSTRING%3E%3B%0A%7D%0A%0Aexport%20function%20RenderProfilePicture(props%3A%20IProfilePicProps)%20%7B%0A%0A%20%20%20%20const%20%5BprofileUrl%2C%20setProfileUrl%5D%20%3D%20React.useState%3CSTRING%3E()%3B%0A%20%20%20%20let%20%7B%20displayName%2C%20getUserProfileUrl%20%7D%20%3D%20props%3B%0A%0A%20%20%20%20React.useEffect(()%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20getUserProfileUrl().then(url%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20setProfileUrl(url)%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D%2C%20%5Bprops%5D)%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20%3CDIV%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CPERSONA%20imageurl%3D%22%7BprofileUrl%7D%22%20text%3D%22%7BdisplayName%7D%22%20size%3D%22%7BPersonaSize.size32%7D%22%20imagealt%3D%22%7BdisplayName%7D%22%20styles%3D%22%7B%7B%22%3E%3C%2FPERSONA%3E%0A%20%20%20%20%20%20%20%20%3C%2FDIV%3E)%3B%0A%7D%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E4.%20Now%20move%20to%20the%20%3CSTRONG%3ESpfxpersona.tsx%3C%2FSTRONG%3E%20our%20main%20component.%20here%20we%20will%20call%20APIs%20and%20render%20the%20child%20component%20here.%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-indent-padding-left-30px%22%3ECreate%20a%20state%20interface%20and%20initialize%20a%20user%20state.%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-indent-padding-left-30px%22%3EAt%20here%20we%20are%20getting%20the%20user%20from%20a%20particular%20group%20ID%20so%20first%20call%20API%20to%20get%20users%20from%20the%20group.%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-indent-padding-left-30px%22%3EAnd%20then%20we%20will%20get%20user%20profile%20properties%20to%20get%20users'%20profile%20picture%20so%20after%20calling%20the%20get%20users%20method%20we%20will%20call%20API%20to%20get%20user%20profile%20properties.%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-indent-padding-left-30px%22%3EAnd%20then%20render%20the%20component.%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-indent-padding-left-30px%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eimport%20*%20as%20React%20from%20'react'%3B%0Aimport%20styles%20from%20'.%2FSpfxpersona.module.scss'%3B%0Aimport%20%7B%20ISpfxpersonaProps%20%7D%20from%20'.%2FISpfxpersonaProps'%3B%0Aimport%20%7B%20escape%20%7D%20from%20'%40microsoft%2Fsp-lodash-subset'%3B%0Aimport%20%7B%20sp%20%7D%20from%20%22%40pnp%2Fsp%2Fpresets%2Fall%22%3B%0Aimport%20%7B%20RenderProfilePicture%20%7D%20from%20'..%2FCommon%2FComponents%2FRenderProfilePicture%2FRenderProfilePicture'%0A%0Aexport%20interface%20ISpfxpersonaWebPartState%20%7B%0A%20%20users%3A%20any%5B%5D%0A%7D%0Aexport%20default%20class%20Spfxpersona%20extends%20React.Component%3CISPFXPERSONAPROPS%3E%20%7B%0A%0A%20%20constructor(props%3A%20ISpfxpersonaProps)%20%7B%0A%20%20%20%20super(props)%3B%0A%20%20%20%20this.state%20%3D%20%7B%0A%20%20%20%20%20%20users%3A%20%5B%5D%0A%20%20%20%20%7D%0A%20%20%7D%0A%0A%20%20private%20async%20getUserProfileUrl(loginName%3A%20string)%20%7B%0A%20%20%20%20const%20userPictureUrl%20%3D%20await%20sp.profiles.getUserProfilePropertyFor(loginName%2C%20'PictureURL')%3B%0A%20%20%20%20return%20userPictureUrl%3B%0A%20%20%7D%0A%0A%20%20private%20async%20getSiteUsers()%20%7B%0A%20%20%20%20const%20grpUsers%20%3D%20await%20sp.web.siteGroups.getById(3).users()%3B%0A%20%20%20%20this.setState(%7B%20users%3A%20grpUsers%20%7D)%0A%20%20%7D%0A%0A%20%20public%20componentDidMount()%20%7B%0A%20%20%20%20this.getSiteUsers()%0A%20%20%7D%0A%0A%20%20public%20render()%3A%20React.ReactElement%3CISPFXPERSONAPROPS%3E%20%7B%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%3CDIV%20classname%3D%22%7Bstyles.spfxpersona%7D%22%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%3EUSERS%20WITH%20PERSONA%20CARD%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%7Bthis.state.users.map(m%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%3CRENDERPROFILEPICTURE%20loginname%3D%22%7Bm.LoginName%7D%22%20displayname%3D%22%7Bm.Title%7D%22%20getuserprofileurl%3D%22%7B()%22%3E%20this.getUserProfileUrl(m.LoginName)%7D%20%20%26gt%3B%3C%2FRENDERPROFILEPICTURE%3E%0A%20%20%20%20%20%20%20%20)%7D%0A%20%20%20%20%20%20%3C%2FDIV%3E%0A%20%20%20%20)%3B%0A%20%20%7D%0A%7D%0A%3C%2FISPFXPERSONAPROPS%3E%3C%2FISPFXPERSONAPROPS%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-indent-padding-left-30px%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%20id%3D%22pastingspan1%22%3ENow%20serve%20the%20application%20using%20the%20below%20command%2C%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CPRE%3Egulp%20serve%3C%2FPRE%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%20id%3D%22pastingspan1%22%3ENow%20test%20the%20webpart%20in%20SharePoint-SiteURL%20%2B%20%2F_layouts%2F15%2Fworkbench.aspx.%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CH2%20id%3D%22toc-hId--1567574760%22%20id%3D%22toc-hId--1569396874%22%3EOutput%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Output.png%22%20style%3D%22width%3A%20315px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F287813iB1FABBA98F6A0A91%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Output.png%22%20alt%3D%22Output.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFind%20the%20full%20source%20code%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fchandaniprajapati%2Fspfx-fluentui-persona%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-919938073%22%20id%3D%22toc-hId-918115959%22%3ESummary%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20article%2C%20we%20have%20seen%20the%20step-by-step%20implementation%20of%20how%20to%20use%20a%20persona%20card%20to%20show%20the%20users'%20profile%20picture.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20hope%20this%20helps%2C%20if%20this%20helps%20you%20then%20share%20it%20with%20others.%3CBR%20%2F%3E%3CBR%20%2F%3ESharing%20is%20caring!%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2415229%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20article%2C%20we%20will%20see%20how%20to%20show%20user%20pictures%20or%20user%20initials%20in%20the%20SPFx%20web%20part%20using%20fluent%20UI%20react%20persona%20control.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2415229%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFluent%20UI%20React%20Controls%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHow%20to%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESPFx%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jun 10 2021 05:51 AM
Updated by:
www.000webhost.com