DEV Community

Cover image for Get SharePoint library info from Teams context
Kinga
Kinga

Posted on

Get SharePoint library info from Teams context

Integration between Power Apps and MS Teams allows creating context-aware applications, that access information stored for example in the SharePoint site connected to the team.

Embedding vs integrating

Embedding Power Apps canvas apps is different from creating Canvas Apps in MS Teams.

A Canvas App created in Teams can only be edited using Power Apps app in MS teams.
The environment is not displayed in https://make.powerapps.com/, but can be found using https://make.powerautomate.com/. This is useful when you need to export solution.👈 Still, you won't be able to edit this app via browser.
The app has access to a Teams integration object, which provides context information, for example team id and group id, and channel id.

Image description

Embedded Canvas App, on the other hand, is a standalone Canvas App, added to MS Teams.

Embedding Canvas Apps in MS Teams, available since March 2020, gives an app access to the team's context information using the Param('parameterName').

Image description

The team Id parameter

Sadly... the id returned by teamId parameter is not a team's guid, but an internalId.

The best explanation of these internalIds can be found in stackoverflow

stackoverflow answer

I find it very unfortunate.
The internal id cannot be used with either the Teams Power Automate actions, or the MS Graph API.
Calling the https://graph.microsoft.com/v1.0/teams?$select=id,internalId returns null for the internalId. The only way of retrieving the guid is indeed retrieving all the teams the user has access to, and for each of them, retrieving the id and internalId values.
In terms of solution design, this seems like an anti pattern...

Moreover, the licensing document states that the M365 licenses provide "limited usage rights" to Power Platform, allowing users to extend the M365 services in order to support business processes. But finding the team's guid requires using Premium connector because the Send a Microsoft Graph HTTP request action does not support https://graph.microsoft.com/v1.0/teams?$select= id,internalId query.

So dissapointing...

Fetching id, driveId, webUrl, etc.

Nevertheless... I decided to build a flow that retrieves all the guids, ids, names and paths that might be useful in building Power Platform solutions.

Depending on which Power Automate action you are using, you will need to know the target location's URL, guid, driveId or list title. I briefly described this topic in my previous post: Data Source Environment Variables in Power Automate actions.

Retrieving these values based on the team and channel internalId requires the following steps:

  • get guids of all teams the user has access to and for each of them, fetch the internalId property. If the internalId is equal to teamId provided by the MS Teams context, this is the team you are looking for. the guid will be used to access team information with MS Graph. Unfortunately, the internalId is null when retrieving all teams with https://graph.microsoft.com/v1.0/teams?$select=id,internalId
  • get SharePoint folder information for the Teams channel, using the guid retrieved in the previous step, and the channelId from team's context. The https://graph.microsoft.com/v1.0/teams/{teamGuid}/channels/{channelId}/filesFolder?$select=id,name,parentReference query returns id, name and parentReference properties, which are used by drives API in the next step.
    results step 1

  • get SharePoint folder information by calling https://graph.microsoft.com/v1.0/drives/{driveId}/items/{id}/listItem?$select=id,webUrl,parentReference. This query returns folder id (integer), absolute URL and a reference to a SharePoint site used by the channel. The parentReference/siteId returns tenantName,SiteId,WebId.
    results step 2

  • get library information using https://graph.microsoft.com/v1.0/drives/{driveId}/list?$select=id,webUrl,name,displayName,parentReference. This query returns library information: display name, internal name which is used as part of the url, library guid and absolute URL
    results step 3

  • get site information using https://graph.microsoft.com/v1.0/sites/{siteId}?$select=name,weburl,displayName
    results step 4

Results

The output may be used in a parent Power Automate flow, to access library and folder used to store channel's documents.

To test it, add the Get Teams Channel Library Info - test Power App to a MS Teams channel.

The top section displays Teams context parameters. The Fetch folder information button triggers the Get Teams Channel Library Info flow, with results displayed below:

Power Apps application screenshot

Prerequisites

This Power Automate Flow is using HTTP with Microsoft Entra ID connector, which requires Premium license.

In order to successfully execute the flow, you need to grant the PowerPlatform-webcontentsv2-Connector service principal the following API permissions to Microsoft Graph resource:

  • Channel.ReadBasic.All
  • Team.ReadBasic.All
  • Files.Read.All

API Permissions

Follow the Authorize the connector to act on behalf of a signed-in user procedure to grant the permissions.

Doesn't work!

If you see a " Missing scope permissions on the request." error, it is possible that you tested the flow before granting the required API permissions. Now you have to wait. According to my experience - approximately one hour.

Download the solution

You may download the workflow from the Power-Automate-Get-Teams-Channel-Library-Info github repository.

The flow accepts two parameters: teamsId and channelId. Depending on the app type, use the following parameters:

Embeded Canvas App Integrated Canvas App
teamId Param("teamId") Teams.ThisTeam.Id
channelId Param("channelId") Teams.ThisChannel.Id

Resources

Top comments (0)