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,
Implementation
Open a command prompt
Move to the path where you want to create a project
Create a project directory using:
md spfx-persona
cd spfx-persona
yo @microsoft/sharepoint
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 .
import { WebPartContext } from "@microsoft/sp-webpart-base";
export interface ISpfxpersonaProps {
description: string;
context: WebPartContext
}
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
})
]
}
]
}
]
};
}
}
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>);
}
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>
);
}
}
gulp serve
Output
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!