Retrieving SharePoint Site URL for Teams Channels
Retrieving SharePoint Site URL for Teams Channels
Have you ever needed to construct the SharePoint site URL for a private or shared channel using just the channel name? This post will guide you through the process using Microsoft Graph endpoint and PnP PowerShell though PowerShell.
You may need the details for these reasons
- To specify within a retention policy Purview Retention Policy Questions from the Field
- eDiscovery Extract from eDiscovery of private and shared channels
“Each private and shared channel has its own SharePoint site that’s separate from the parent team site. That means files in private and shared channels are stored in its own site and managed independently of the parent team. This means you must identify and search the specific site associated with a channel when searching for content in files and channel message attachments.”
The Site Urls associated with a private or shared channel can be included as part of eDiscovery search query in Search for content in an eDiscovery (Standard) case
- Configure the site by applying a site design/template
Private Channels
The default SharePoint URL for a private channel follows the format: <ParentSharePointSiteUrl><ChannelRelativeUrl>
. Note that if the channel is renamed, the corresponding SharePoint URL remains unchanged.
Shared Channels
Similarly, the default SharePoint URL for a shared channel follows the format: <ParentM365GroupName><ChannelRelativeUrl>
. Again, renaming the channel does not alter the SharePoint URL.
At times, you may need to access the underlying SharePoint object representing the Channel folder in SharePoint for specific operations. You can retrieve the SharePoint URL of the channel by querying the Graph API at beta/teams/$m365GroupId/channels/$($channel.Id)
.
PowerShell Script to Retrieve All Teams Channel SharePoint Site URLs Linked to M365 Connected Teams Site
It can be useful just to extract the site urls of associated channels to a Teams for various reasons:
$m365SiteUrl = "https://contoso.sharepoint.com/sites/test"
Connect-PnPOnline -url $url -interactive
$m365GroupId = (get-pnpsite -include RelatedGroupId).RelatedGroupId.Guid
$RestMethodUrl = "beta/teams/$m365GroupId/channels"
$channels = (Invoke-PnPGraphMethod -Url $RestMethodUrl -Method Get -ConsistencyLevelEventual).value
$channels | ForEach-Object {
write-host ($_.filesfolderweburl).split("Shared Documents")[0]
}
Alternative script to not call call the beta endpoint for Microsoft Graph
param (
[Parameter(Mandatory = $true)]
[string] $domain ,
[Parameter(Mandatory = $true)]
[string] $teamName
)
$adminSiteURL = "https://$domain-Admin.SharePoint.com"
Connect-PnPOnline -Url $adminSiteURL
$team = Get-PnPTeamsTeam -Identity $teamName
$m365GroupId = $team.GroupId
Get-PnPTenantSite | Where-Object { $_.Template -eq 'TEAMCHANNEL#1' -and $_.RelatedGroupId -eq $m365GroupId } | select Url,Template, Title
PowerShell Script to Retrieve SharePoint Site URL for a Specific Teams Channel
function get-channelsiteurl ($m365GroupId, $channelName){
$channel= get-pnpteamschannel -team $m365GroupId -Identity $channelName
$RestMethodUrl = "beta/teams/$m365GroupId/channels/$($channel.Id)"
$channel = Invoke-PnPGraphMethod -Url $RestMethodUrl -Method Get -ConsistencyLevelEventual
return ($channel.filesfolderweburl).split("Shared Documents")[0]
}
$url = "https://contoso.sharepoint.com/teams/d-TEAM-DemoTeam"
Connect-PnPOnline -url $url -interactive
$m365GroupId = (get-pnpsite -include RelatedGroupId).RelatedGroupId.Guid
get-channelsiteurl $m365GroupId "Ash-Test"
Conclusion
The Graph API can be used to access Teams Channels and SharePoint folders. Regardless of whether the channel has been renamed or is a private channel, you can always retrieve the URL to the folder in SharePoint using the endpoint beta/teams/$m365GroupId/channels/$($channel.Id).
References
Get a Teams channel SharePoint Url using Graph API