Group members with presence information (#1091)

* sample v0.1 added

* Readme file content and demo image

* Adding view tracking image to readme

Co-authored-by: Vesa Juvonen <VesaJuvonen@users.noreply.github.com>
This commit is contained in:
Luis Manez 2019-12-30 14:59:33 +01:00 committed by Vesa Juvonen
parent a18b0e5f73
commit 1258e21ca0
29 changed files with 18778 additions and 0 deletions

View File

@ -0,0 +1,25 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
# change these settings to your own preference
indent_style = space
indent_size = 2
# we recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
[{package,bower}.json]
indent_style = space
indent_size = 2

View File

@ -0,0 +1,32 @@
# Logs
logs
*.log
npm-debug.log*
# Dependency directories
node_modules
# Build generated files
dist
lib
solution
temp
*.sppkg
# Coverage directory used by tools like istanbul
coverage
# OSX
.DS_Store
# Visual Studio files
.ntvs_analysis.dat
.vs
bin
obj
# Resx Generated Code
*.resx.ts
# Styles Generated Code
*.scss.ts

View File

@ -0,0 +1,12 @@
{
"@microsoft/generator-sharepoint": {
"isCreatingSolution": true,
"environment": "spo",
"version": "1.9.1",
"libraryName": "react-members-with-presence",
"libraryId": "783b6d77-2560-4531-b37c-9a5162196d92",
"packageManager": "npm",
"isDomainIsolated": false,
"componentType": "webpart"
}
}

View File

@ -0,0 +1,56 @@
# Spfx Webpart Group members list with Presence information
## Summary
This sample shows how to get the members of a specific group, including their presence information (using the new Presence endpoint in the MS Graph API).
![Members with Presence](./assets/react-members-with-presence.gif)
## Used SharePoint Framework Version
![SPFx v1.9.1](https://img.shields.io/badge/SPFx-1.9.1-green.svg)
## Applies to
* [SharePoint Framework Developer](http://dev.office.com/sharepoint/docs/spfx/sharepoint-framework-overview)
* [Office 365 developer tenant](http://dev.office.com/sharepoint/docs/spfx/set-up-your-developer-tenant)
## Solution
Solution|Author(s)
--------|---------
react-members-with-presence|Luis Mañez (MVP, [ClearPeople](http://www.clearpeople.com), @luismanez)
## Version history
Version|Date|Comments
-------|----|--------
1.0.0|Dec 23, 2019|Initial release
## Disclaimer
**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.**
---
## Minimal Path to Awesome
* clone repo
* Using the Office365 CLI, connect to your tenant and run the following commands (this will add the proper permissions to the Tenant, in order to call Graph API):
- spo serviceprincipal grant add --resource "Microsoft Graph" --scope "User.Read.All"
- spo serviceprincipal grant add --resource "Microsoft Graph" --scope "Presence.Read.All"
* run _gulp serve_
* open the SharePoint workbench in a Modern Team site (Communication site does not have a Group)
## Features
This sample shows how to get the members of a specific Group and their Presence information.
This sample illustrates the following concepts on top of the SharePoint Framework:
* Using GraphHttpClient to get data from MS Graph API
* How to get Presence information using MS Graph API
* How to configure API Permissions using Office365 CLI
* Using async / await for the async calls
* Office UI fabric components
<img src="https://telemetry.sharepointpnp.com/sp-dev-fx-webparts/samples/react-members-with-presence" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

View File

@ -0,0 +1,18 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/config.2.0.schema.json",
"version": "2.0",
"bundles": {
"people-with-presence-web-part": {
"components": [
{
"entrypoint": "./lib/webparts/peopleWithPresence/PeopleWithPresenceWebPart.js",
"manifest": "./src/webparts/peopleWithPresence/PeopleWithPresenceWebPart.manifest.json"
}
]
}
},
"externals": {},
"localizedResources": {
"PeopleWithPresenceWebPartStrings": "lib/webparts/peopleWithPresence/loc/{locale}.js"
}
}

View File

@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/copy-assets.schema.json",
"deployCdnPath": "temp/deploy"
}

View File

@ -0,0 +1,7 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/deploy-azure-storage.schema.json",
"workingDir": "./temp/deploy/",
"account": "<!-- STORAGE ACCOUNT NAME -->",
"container": "react-members-with-presence",
"accessKey": "<!-- ACCESS KEY -->"
}

View File

@ -0,0 +1,13 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
"solution": {
"name": "react-members-with-presence-client-side-solution",
"id": "783b6d77-2560-4531-b37c-9a5162196d92",
"version": "1.0.0.0",
"includeClientSideAssets": true,
"isDomainIsolated": false
},
"paths": {
"zippedPackage": "solution/react-members-with-presence.sppkg"
}
}

View File

@ -0,0 +1,10 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",
"port": 4321,
"https": true,
"initialPage": "https://localhost:5432/workbench",
"api": {
"port": 5432,
"entryPath": "node_modules/@microsoft/sp-webpart-workbench/lib/api/"
}
}

View File

@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/write-manifests.schema.json",
"cdnBasePath": "<!-- PATH TO CDN -->"
}

View File

@ -0,0 +1,7 @@
'use strict';
const gulp = require('gulp');
const build = require('@microsoft/sp-build-web');
build.addSuppression(`Warning - [sass] The local CSS class 'ms-Grid' is not camelCase and will not be type-safe.`);
build.initialize(gulp);

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,41 @@
{
"name": "react-members-with-presence",
"version": "0.0.1",
"private": true,
"main": "lib/index.js",
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"build": "gulp bundle",
"clean": "gulp clean",
"test": "gulp test"
},
"dependencies": {
"react": "16.8.5",
"react-dom": "16.8.5",
"@types/react": "16.8.8",
"@types/react-dom": "16.8.3",
"office-ui-fabric-react": "6.189.2",
"@microsoft/sp-core-library": "1.9.1",
"@microsoft/sp-webpart-base": "1.9.1",
"@microsoft/sp-lodash-subset": "1.9.1",
"@microsoft/sp-office-ui-fabric-core": "1.9.1",
"@types/webpack-env": "1.13.1",
"@types/es6-promise": "0.0.33"
},
"resolutions": {
"@types/react": "16.8.8"
},
"devDependencies": {
"@microsoft/sp-build-web": "1.9.1",
"@microsoft/sp-tslint-rules": "1.9.1",
"@microsoft/sp-module-interfaces": "1.9.1",
"@microsoft/sp-webpart-workbench": "1.9.1",
"@microsoft/rush-stack-compiler-2.9": "0.7.16",
"gulp": "~3.9.1",
"@types/chai": "3.4.34",
"@types/mocha": "2.2.38",
"ajv": "~5.2.2"
}
}

View File

@ -0,0 +1 @@
// A file is required to be in the root of the /src directory by the TypeScript compiler

View File

@ -0,0 +1,27 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx/client-side-web-part-manifest.schema.json",
"id": "84ae7796-9c5f-4186-af52-7ccd831f551b",
"alias": "PeopleWithPresenceWebPart",
"componentType": "WebPart",
// The "*" signifies that the version should be taken from the package.json
"version": "*",
"manifestVersion": 2,
// If true, the component can only be installed on sites where Custom Script is allowed.
// Components that allow authors to embed arbitrary script code should set this to true.
// https://support.office.com/en-us/article/Turn-scripting-capabilities-on-or-off-1f2c515f-5d7e-448a-9fd7-835da935584f
"requiresCustomScript": false,
"supportedHosts": ["SharePointWebPart"],
"preconfiguredEntries": [{
"groupId": "5c03119e-3074-46fd-976b-c60198311f70", // Other
"group": { "default": "Other" },
"title": { "default": "PeopleWithPresence" },
"description": { "default": "Showing the site members and owners with the presence info (from MS Graph API)" },
"officeFabricIconFontName": "Page",
"properties": {
"description": "PeopleWithPresence"
}
}]
}

View File

@ -0,0 +1,78 @@
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField
} from '@microsoft/sp-webpart-base';
import { MSGraphClient } from '@microsoft/sp-http';
import * as strings from 'PeopleWithPresenceWebPartStrings';
import PeopleWithPresence from './components/PeopleWithPresence';
import { IPeopleWithPresenceProps } from './components/IPeopleWithPresenceProps';
export interface IPeopleWithPresenceWebPartProps {
description: string;
}
export default class PeopleWithPresenceWebPart extends BaseClientSideWebPart<IPeopleWithPresenceWebPartProps> {
private _graphHttpClient: MSGraphClient;
protected onInit(): Promise<void> {
return new Promise((resolve, reject) => {
this.context.msGraphClientFactory.getClient().then(client => {
this._graphHttpClient = client;
resolve();
}).catch(error => {
console.log(error);
reject(error);
});
});
}
public render(): void {
const element: React.ReactElement<IPeopleWithPresenceProps > = React.createElement(
PeopleWithPresence,
{
graphHttpClient: this._graphHttpClient,
siteUrl: this.context.pageContext.site.absoluteUrl,
groupId: this.context.pageContext.site.group.id
}
);
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
})
]
}
]
}
]
};
}
}

View File

@ -0,0 +1,8 @@
import { MSGraphClient } from '@microsoft/sp-http';
import { Guid } from '@microsoft/sp-core-library';
export interface IPeopleWithPresenceProps {
graphHttpClient: MSGraphClient;
siteUrl: string;
groupId: Guid;
}

View File

@ -0,0 +1,6 @@
import { IPerson } from "../models/IPerson";
import { Dictionary } from "../models/Dictionary";
export interface IPeopleWithPresenceState {
members: Dictionary<IPerson>;
}

View File

@ -0,0 +1,74 @@
@import '~office-ui-fabric-react/dist/sass/References.scss';
.peopleWithPresence {
.container {
max-width: 700px;
margin: 0px auto;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
}
.row {
@include ms-Grid-row;
@include ms-fontColor-white;
background-color: $ms-color-themeDark;
padding: 20px;
}
.column {
@include ms-Grid-col;
@include ms-lg10;
@include ms-xl8;
@include ms-xlPush2;
@include ms-lgPush1;
}
.title {
@include ms-font-xl;
@include ms-fontColor-white;
}
.subTitle {
@include ms-font-l;
@include ms-fontColor-white;
}
.description {
@include ms-font-l;
@include ms-fontColor-white;
}
.button {
// Our button
text-decoration: none;
height: 32px;
// Primary Button
min-width: 80px;
background-color: $ms-color-themePrimary;
border-color: $ms-color-themePrimary;
color: $ms-color-white;
// Basic Button
outline: transparent;
position: relative;
font-family: "Segoe UI WestEuropean","Segoe UI",-apple-system,BlinkMacSystemFont,Roboto,"Helvetica Neue",sans-serif;
-webkit-font-smoothing: antialiased;
font-size: $ms-font-size-m;
font-weight: $ms-font-weight-regular;
border-width: 0;
text-align: center;
cursor: pointer;
display: inline-block;
padding: 0 16px;
.label {
font-weight: $ms-font-weight-semibold;
font-size: $ms-font-size-m;
height: 32px;
line-height: 32px;
margin: 0 4px;
vertical-align: top;
display: inline-block;
}
}
}

View File

@ -0,0 +1,139 @@
import * as React from 'react';
import styles from './PeopleWithPresence.module.scss';
import { IPeopleWithPresenceProps } from './IPeopleWithPresenceProps';
import { IPeopleWithPresenceState } from './IPeopleWithPresenceState';
import { IPerson } from '../models/IPerson';
import { Dictionary } from '../models/Dictionary';
import { Persona, PersonaSize, PersonaPresence } from 'office-ui-fabric-react/lib/Persona';
import { Stack } from 'office-ui-fabric-react/lib/Stack';
export default class PeopleWithPresence extends React.Component<IPeopleWithPresenceProps, IPeopleWithPresenceState> {
constructor(props: IPeopleWithPresenceProps) {
super(props);
this.state = {
members: undefined
};
}
private async _getMembers(): Promise<Dictionary<IPerson>> {
const endpoint: string = `https://graph.microsoft.com/beta/groups/${this.props.groupId}/members?$select=id,displayName,department`;
const response: any = await this.props.graphHttpClient.api(endpoint).get();
const graphResponse: any = response.value;
let peopleDictionary: Dictionary<IPerson> = {};
graphResponse.map(user => {
const person: IPerson = {
id: user.id, displayName: user.displayName, department: user.department
};
peopleDictionary[person.id.toString()] = person;
});
return peopleDictionary;
}
private async _getMembersPresense(users: string[]): Promise<Dictionary<IPerson>> {
const endpoint: string = `https://graph.microsoft.com/beta/communications/getPresencesByUserId`;
const response: any = await this.props.graphHttpClient.api(endpoint).post({
"ids": users
});
const graphResponse: any = response.value;
let peopleDictionary: Dictionary<IPerson> = {};
graphResponse.map(user => {
const person: IPerson = {
id: user.id, displayName: '', department: '', availability: user.availability, activity: user.activity
};
peopleDictionary[person.id.toString()] = person;
});
return peopleDictionary;
}
public async componentDidMount(): Promise<void> {
const members: Dictionary<IPerson> = await this._getMembers();
let users: string[] = this.getMemberIds(members);
const presenceInfo: Dictionary<IPerson> = await this._getMembersPresense(users);
this.completeMembersListWithPresenceInfo(members, presenceInfo);
this.setState({
members: members
});
}
private completeMembersListWithPresenceInfo(members: Dictionary<IPerson>, presenceInfo: Dictionary<IPerson>) {
for (let key in members) {
members[key].activity = presenceInfo[key].activity;
members[key].availability = presenceInfo[key].availability;
}
}
private getMemberIds(members: Dictionary<IPerson>) {
let users: string[] = [];
for (let key in members) {
users.push(key);
}
return users;
}
public render(): React.ReactElement<IPeopleWithPresenceProps> {
let members = [<div>Getting members...</div>];
if (this.state.members) {
let membersArray: IPerson[] = [];
for (let key in this.state.members) {
let value = this.state.members[key];
membersArray.push(value);
}
members = membersArray.map(member => {
return (
<Persona
size={PersonaSize.size72}
text={member.displayName}
secondaryText={member.department}
tertiaryText={member.activity}
presence={this._fromPresenceAvailabilityToPersonaPresence(member.availability)} />);
});
}
return (
<Stack>
<h1>Members</h1>
{members}
</Stack>
);
}
private _fromPresenceAvailabilityToPersonaPresence(availability: string): PersonaPresence {
switch (availability) {
case 'Busy':
case 'BusyIdle':
return PersonaPresence.busy;
case 'Available':
case 'AvailableIdle':
return PersonaPresence.online;
case 'Away':
case 'BeRightBack':
return PersonaPresence.away;
case 'Offline':
return PersonaPresence.offline;
case 'DoNotDisturb':
return PersonaPresence.dnd;
default:
return PersonaPresence.none;
}
}
}

View File

@ -0,0 +1,7 @@
define([], function() {
return {
"PropertyPaneDescription": "Description",
"BasicGroupName": "Group Name",
"DescriptionFieldLabel": "Description Field"
}
});

View File

@ -0,0 +1,10 @@
declare interface IPeopleWithPresenceWebPartStrings {
PropertyPaneDescription: string;
BasicGroupName: string;
DescriptionFieldLabel: string;
}
declare module 'PeopleWithPresenceWebPartStrings' {
const strings: IPeopleWithPresenceWebPartStrings;
export = strings;
}

View File

@ -0,0 +1,3 @@
export interface Dictionary<T> {
[key: string]: T | undefined;
}

View File

@ -0,0 +1,9 @@
import { Guid } from "@microsoft/sp-core-library";
export interface IPerson {
id: Guid;
displayName: string;
department?: string;
availability?: string;
activity?: string;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -0,0 +1,38 @@
{
"extends": "./node_modules/@microsoft/rush-stack-compiler-2.9/includes/tsconfig-web.json",
"compilerOptions": {
"target": "es5",
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"jsx": "react",
"declaration": true,
"sourceMap": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"outDir": "lib",
"inlineSources": false,
"strictNullChecks": false,
"noUnusedLocals": false,
"typeRoots": [
"./node_modules/@types",
"./node_modules/@microsoft"
],
"types": [
"es6-promise",
"webpack-env"
],
"lib": [
"es5",
"dom",
"es2015.collection"
]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"lib"
]
}

View File

@ -0,0 +1,30 @@
{
"extends": "@microsoft/sp-tslint-rules/base-tslint.json",
"rules": {
"class-name": false,
"export-name": false,
"forin": false,
"label-position": false,
"member-access": true,
"no-arg": false,
"no-console": false,
"no-construct": false,
"no-duplicate-variable": true,
"no-eval": false,
"no-function-expression": true,
"no-internal-module": true,
"no-shadowed-variable": true,
"no-switch-case-fall-through": true,
"no-unnecessary-semicolons": true,
"no-unused-expression": true,
"no-use-before-declare": true,
"no-with-statement": true,
"semicolon": true,
"trailing-comma": false,
"typedef": false,
"typedef-whitespace": false,
"use-named-parameter": true,
"variable-name": false,
"whitespace": false
}
}