Epic Visio Skills produced this cool illustration of Microsoft Teams extensibility!

How to Teamsify an SPFx solution?

This post was most recently updated on August 14th, 2019.

Reading Time: 6 minutes.

This post describes how you can modify your SPFx webparts to make them compatible with usage in Microsoft Teams. I’m also showing some basic ideas what you can do in the code to make the integration more useful!

Why would we do this?

Who wouldn’t want to just develop once and then run their code everywhere? Now with SPFx (SharePoint Framework) 1.8 being out (and 1.9 being out for a while before being pulled!), we’re getting one step closer to that, as we’re given an elegant way to bridge the gap between your portals built on SharePoint, and Microsoft Teams where most of the collaboration happens. Teams is just getting more and more powerful – and you can make it even more powerful by extending it.

So, now you can just develop your functionality once, and then run it everywhere! Everywhere, in this case, being SharePoint and Teams. But it’s a start!

What’s SharePoint Framework (SPFx)?

Let’s take a look at the basics – what’s the point of SharePoint Framework?

SharePoint Framework in a nutshell:

  • Client-side execution model
  • Rendered & executed in page context
  • Only way to customize modern pages
  • Open source cross-platform tooling
  • Responsive, accessible & mobile friendly
  • Scoped to tenant or site
  • Limited to creating client-side web parts & UI extensions

With Microsoft Teams, the situation is similar. An SPFx webpart can be hosted on a Modern SharePoint site and surfaced in a tab in Teams.

Okay – so we know the basics, now let’s jump into the action!

Getting started on teamsifying your SPFx solutions

Yeah, so I’m calling this Teamsifying, and there’s nothing you can do about it, so let’s just get past it and into the process!

Whatever your normal SPFx development flow is, typically you’ll be able to adhere to it. In my example, I’m using Visual Studio Code – let’s see how it goes.

Getting started

First of all, if you’re sideloading apps (which is what we’re doing today), you’ll need to enable Teams development on your tenant. You can do this by accessing the development mode from your profile menu in Teams:

And then accept by selecting “Switch to developer preview”.

Now you’re ready to load your custom thingies into Microsoft Teams!

While we’re at it, let’s make this useful!

First of all, let’s use an existing Modern Calendar webpart, that we have on our tenant. Why? Because weirdly enough, Teams lacks a Calendar app for a team!

Weird huh? But it’s not a problem, since you can always use the SPFx webparts developed for your SharePoint sites! That way, we’ll be able to use some Open Source code to bring new useful functionality into both SharePoint and Teams at once!

Steps for getting your SPFx webpart into Microsoft Teams

So, let’s jump into it!

1. Creating a Teams manifest for your solution

This step is currently unnecessarily complicated – but will be more straightforward in the future for sure.

Basically, you’ll need to craft a zipped folder, that’ll act as a manifest and icons for the app that we’ll register with Teams.

The easiest way to do this is to download a premade .zip -package, that we’ll be then modifying. A suitable example of such file can be downloaded from my GitHub account, for example.

After you’ve got the package, unzip it as “teams” folder (just a convention – you can actually name it whatever you want) in your solution. When you open the file called manifest.json, you need to replace a couple of things inside it – see below:

There's a few things you'll need to replace from the Teams manifest file. First of all, the componentId (number 1) needs to be replaced to be something unique. Safest to generate a new guid. Then make sure that the scopes (number 2) includes "teams". Finally, the guid in the path (number 3) needs to be replaced with the id (guid) of the SPFx component - you can get that from the SPFx component's manifest file!
There’s a few things you’ll need to replace from the Teams manifest file. First of all, the componentId (number 1) needs to be replaced to be something unique. Safest to generate a new guid. Then make sure that the scopes (number 2) includes “teams”. Finally, the guid in the path (number 3) needs to be replaced with the id (guid) of the SPFx component – you can get that from the SPFx component’s manifest file!

You can find the guid that you’ll need from another file, called YourWebPartname.manifest.json. See an example below!

You'll need to grab the guid of this component from the manifest file - see the highlighted parts. It's needed for the Teams manifest.
You’ll need to grab the guid of this component from the manifest file – see the highlighted parts. It’s needed for the Teams manifest.

When that’s done, go ahead and zip the files back up – you’ll need the manifest and the 2 icons in.

Then navigate to your team in Microsoft Teams, and select “Manage Team”.

After that, select Apps and on the right bottom hand of the screen, there’s a text “Upload a custom app”. Select that.

Now select your zip package, and give it a go. If you get an error about the manifest being malformed, verify 2 things:

  1. You zipped the right thing (should only contain the 3 files, no folders or anything)
  2. You didn’t mess up the manifest file’s syntax (use a tool such as VSCode to verify it’s not broken, or just grab an unmodified one and try again)

After that, you should be able to add a new tab with the name you specified for your app! You can do that either from the app listing, or from the channel view of your team.

So, now you’ve got the SPFx webpart in your Teams. Great job! However, that’s only half of the fun.

2. Update the SPFx webpart to use at least SPFx 1.8

To make the webpart work nicely with your channels, there’s another thing we can do. We can grab the actual Teams context and access some properties from the Team the webpart is now added to.

To achieve this, You’ll need to update the webpart to SPFx 1.8+. I’ve posted about this before, so while the basic idea is to run this in your solution:

npm install @microsoft/[email protected] --save
npm install @microsoft/[email protected] --save
npm install @microsoft/[email protected] --save
npm install @microsoft/[email protected] --save
gulp --update

You can find more info (there’s plenty more, I assure you) here:

so – with that done, let’s add the code to actually do something with the context!

3. Add functionality that actually integrates the webpart into Teams

First, let’s grab the context! You’ll need to import the namespace:

import * as microsoftTeams from '@microsoft/teams-js';

Then add a private variable where we’ll store the context, and grab it when we’re running the OnInit(). Below, I’m adding the private right after declaring the webpart class:

export default class ModernCalendarWebPart extends BaseClientSideWebPart<IModernCalendarWebPartProps> {
 
  private _teamsContext: microsoftTeams.Context; 
</p>
<!-- /wp:html -->
 
<!-- wp:paragraph -->
<p>If you already have code in the OnInit method, you'll need to add this:</p>
<!-- /wp:paragraph -->
 
<!-- wp:html -->
<pre lang="typescript">protected onInit(): Promise {
    let retVal: Promise = Promise.resolve();
    if (this.context.microsoftTeams) {
      retVal = new Promise((resolve, reject) =&gt; {
        this.context.microsoftTeams.getContext(context =&gt; {
          this._teamsContext = context;
          resolve();
        });
      });
    }
    return retVal;

After this, let’s do something with the context! To just show a simple example, in this example (inside render() -function), if we have a dark theme coming from the Teams context, we’ll flip the colors in the webpart’s styles as well:

public render(): void {
 
    /// ...
 
    if (this._teamsContext) {
      // We have teams context for the web part
      if (this._teamsContext.theme.toLowerCase() == "dark") jQuery("#spPageChromeAppDiv").css("filter","invert(100%)");
    }
 
    // ...
}

And we should be done!

References

https://docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/get-started/using-web-part-as-ms-teams-tab

Antti K. Koskela

Antti Koskela is a proud digital native nomadic millennial full stack developer (is that enough funny buzzwords? That's definitely enough funny buzzwords!), who works as a Solutions Architect for Valo Intranet, the product that will make you fall in love with your intranet. Working with the global partner network, he's responsible for the success of Valo deployments happening all around the world.

He's been a developer from 2004 (starting with PHP and Java), and he's been bending and twisting SharePoint into different shapes since MOSS. Nowadays he's not only working on SharePoint, but also on .NET projects, Azure, Office 365 and a lot of other stuff.

This is his personal professional (e.g. professional, but definitely personal) blog.
mm

1
Leave a Reply

avatar
5000
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
0 Comment authors
Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
trackback
SharePoint Dev Weekly - Episode 47 - Microsoft 365 Developer Blog

[…] #SharePointProblems – How to Teamsify an SPFx solution? – Antti Koskela (Valo Intranet) […]