Skip to content

Commit daa7677

Browse files
Adds information about exposing SPFx web parts as messaging extensions (SharePoint#5988)
1 parent fdb85d0 commit daa7677

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed
Loading

docs/spfx/build-for-teams-expose-webparts-teams.md

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,141 @@
11
---
22
title: Expose SharePoint Framework web parts in Microsoft Teams
33
description: You can expose web parts built using SharePoint Framework in Microsoft Teams.
4-
ms.date: 06/15/2020
4+
ms.date: 07/02/2020
55
ms.prod: sharepoint
66
localization_priority: Normal
77
---
88

99
# Expose SharePoint Framework web parts in Microsoft Teams
1010

11-
Using SharePoint Framework, you can build [web parts](web-parts/overview-client-side-web-parts.md) and [extensions](extensions/overview-extensions.md). When building web parts, you can enable them to be exposed as a Microsoft Teams tab or a personal app.
11+
Using SharePoint Framework, you can build [web parts](web-parts/overview-client-side-web-parts.md) and [extensions](extensions/overview-extensions.md). When building web parts, you can enable them to be exposed as a [Microsoft Teams tab](https://docs.microsoft.com/microsoftteams/platform/tabs/what-are-tabs), a [personal app](https://docs.microsoft.com/microsoftteams/platform/concepts/design/personal-apps) or a [messaging extension](https://docs.microsoft.com/microsoftteams/platform/messaging-extensions/what-are-messaging-extensions).
1212

1313
> [!TIP]
1414
> To see how to use the different concepts described in this article, see the sample [Leads application](https://github.com/pnp/sp-dev-solutions/tree/master/solutions/LeadsLOBSolution) on GitHub.
1515
16+
## Expose web part as Microsoft Teams tab
17+
1618
To expose a SharePoint Framework client-side web part as a Microsoft Teams tab, in the web part’s manifest, in the `supportedHosts` property, add `TeamsTab`.
1719

1820
![TeamsTab host added to the supportedHosts property in a web part manifest](../images/build-for-teams/build-for-teams-manifest-teamstab.png)
1921

22+
## Expose web part as Microsoft Teams personal app
23+
2024
To expose the web part as a Microsoft Teams personal app, in the web part’s manifest, in the `supportedHosts` property, add `TeamsPersonalApp`.
2125

2226
![TeamsPersonalApp host added to the supportedHosts property in a web part manifest](../images/build-for-teams/build-for-teams-manifest-teamspersonalapp.png)
2327

2428
> [!TIP]
2529
> The same SharePoint Framework web part can be exposed as a web part in SharePoint, Microsoft Teams tab and a personal Teams app. The values specified in the `supportedHosts` property decide how users will be able to work with your web part.
2630
31+
## Expose web part as Microsoft Teams messaging extension
32+
33+
To expose your SharePoint Framework web part as a messaging extension, you don't need to use a specific host in the `supportedHosts` property. Instead, all you need to do, is to extend the teams manifest in your SharePoint Framework solution with a `composeExtension`, for example:
34+
35+
```json
36+
{
37+
"$schema": "https://developer.microsoft.com/json-schemas/teams/v1.5/MicrosoftTeams.schema.json",
38+
"manifestVersion": "1.5",
39+
// trimmed for brevity
40+
"composeExtensions": [
41+
{
42+
"botId": "a349bab7-f895-4d6e-977a-764779833699",
43+
"canUpdateConfiguration": true,
44+
"commands": [
45+
{
46+
"id": "shareLead",
47+
"type": "action",
48+
"title": "Share a lead",
49+
"description": "Find and share a lead",
50+
"initialRun": false,
51+
"fetchTask": false,
52+
"context": [
53+
"commandBox",
54+
"compose"
55+
],
56+
"taskInfo": {
57+
"title": "Share a lead",
58+
"width": "1100",
59+
"height": "665",
60+
"url": "https://{teamSiteDomain}/_layouts/15/TeamsLogon.aspx?SPFX=true&dest=/_layouts/15/teamstaskhostedapp.aspx%3Fteams%26personal%26componentId=e81a1b68-686e-412f-90ac-cb80f2544398%26forceLocale={locale}"
61+
}
62+
}
63+
]
64+
}
65+
]
66+
// trimmed for brevity
67+
}
68+
```
69+
70+
The key piece of information is the URL in the `taskInfo` property, which must match the URL specified in the example and which should have the `componentId` query string parameter set to the ID of the SharePoint Framework web part that should be exposed in the messaging extension.
71+
72+
![SharePoint Framework webp part exposed as a messaging extension](../images/build-for-teams/build-for-teams-messaging-extension.gif)
73+
74+
### Responding to user interaction
75+
76+
When your web part is exposed in Microsoft Teams as a messaging extension, you might want to respond to user interaction, for example by posting an adaptive card to the conversation. This requires using a [task module](https://docs.microsoft.com/microsoftteams/platform/task-modules-and-cards/what-are-task-modules) and a [bot](https://docs.microsoft.com/microsoftteams/platform/bots/what-are-bots). The task module notifies the bot of the event that the user triggered, and the bot will post data back to the conversation. First however, you need to check if the web part is used as a messaging extension.
77+
78+
To check if your web part is being used as a messaging extension, check if the `context._host._teamsManager._appContext.applicationName` property is set to `TeamsTaskModuleApplication`:
79+
80+
```typescript
81+
private leadClicked = (ev?: React.SyntheticEvent<HTMLElement>): void => {
82+
const host: string = this.props.host._teamsManager._appContext.applicationName;
83+
if (host !== 'TeamsTaskModuleApplication') {
84+
return;
85+
}
86+
87+
// web part used as a messaging extension
88+
}
89+
```
90+
91+
[see full code](https://github.com/pnp/sp-dev-solutions/blob/cd3757ac071e2fb9f90a3f64b43ede8b1de39a0c/solutions/LeadsLOBSolution/webpart/src/webparts/leads/components/Leads/Leads.tsx#L111-L114)
92+
93+
Once you verified that the web part is used as a messaging extension, you use a task module, to pass the data from the web part to the bot:
94+
95+
```typescript
96+
private leadClicked = (ev?: React.SyntheticEvent<HTMLElement>): void => {
97+
const host: string = this.props.host._teamsManager._appContext.applicationName;
98+
if (host !== 'TeamsTaskModuleApplication') {
99+
return;
100+
}
101+
102+
// web part used as a messaging extension
103+
this.props.teamsContext.tasks.submitTask(selectedLead[0]);
104+
}
105+
```
106+
107+
[see full code](https://github.com/pnp/sp-dev-solutions/blob/cd3757ac071e2fb9f90a3f64b43ede8b1de39a0c/solutions/LeadsLOBSolution/webpart/src/webparts/leads/components/Leads/Leads.tsx#L128)
108+
109+
After receiving the notification, the bot can process the retrieved data and post some information to the conversation.
110+
111+
```typescript
112+
protected async handleTeamsMessagingExtensionSubmitAction(context: TurnContext, action: MessagingExtensionAction): Promise<MessagingExtensionActionResponse> {
113+
const lead: Lead = action.data;
114+
let leadChangeIcon: string = "";
115+
if (lead.change > 0) {
116+
leadChangeIcon = "🔼 ";
117+
} else if (lead.change < 0) {
118+
leadChangeIcon = "🔽 ";
119+
}
120+
121+
const leadCard = CardFactory.adaptiveCard({
122+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
123+
"type": "AdaptiveCard",
124+
"version": "1.0",
125+
// trimmed for brevity
126+
});
127+
128+
await context.sendActivity({ attachments: [leadCard] });
129+
130+
return Promise.resolve({});
131+
}
132+
```
133+
134+
[see full code](https://github.com/pnp/sp-dev-solutions/blob/cd3757ac071e2fb9f90a3f64b43ede8b1de39a0c/solutions/LeadsLOBSolution/bot/src/app/leadsBot/LeadsBot.ts#L24)
135+
136+
> [!TIP]
137+
> For more information about building Microsoft Teams messaging extensions see the [Microsoft Teams documentation](https://docs.microsoft.com/microsoftteams/platform/messaging-extensions/what-are-messaging-extensions). To see an example of how a SharePoint Framework web part is exposed as a messaging extension see the sample [Leads application](https://github.com/pnp/sp-dev-solutions/tree/master/solutions/LeadsLOBSolution) on GitHub.
138+
27139
When you choose to expose your SharePoint Framework web parts in Microsoft Teams, you have a number of options for how to deploy them to Microsoft Teams.
28140

29141
## Use the autogenerated Microsoft Teams manifest

0 commit comments

Comments
 (0)