Forum Discussion
sgsmittal
May 05, 2020Copper Contributor
CreateChannel in GraphAPI sometimes results in not creating the corresponding Sharepoint/OneDrive
Currently the creation of a channel through the Graph API frequently results in the relative, corresponding Sharepoint folder not being created. When using the Graph API to create a channel the resp...
dcc_at_crit
Mar 11, 2025Copper Contributor
I ran into the problem where I could create the Team with a General channel and then channels for each of our departments but the SharePoint sites for the channels where not created without manually clicking the Files tab in the Teams app. I was able to programmatically create the channel SharePoint sites by just calling the filesFolder for the General channel prior to creating the channels for the departments. Then when I created the channels, the sites were automatically created correctly. Below is very simple code I wrote. It by far isn't polished but more a proof of concept. I hope this will help someone else.
$departments = @("BD","Clinical Operations","Data","Finance","HR","IT","Legal","QA")
$companyName = "{YOUR COMPANY URL}"
$companyEMailDomain = "yourdoamin.com"
# We use an application and certificates for authentication - out of scope for this question
Connect-MgGraph -NoWelcome -CertificateThumbprint $cert.Thumbprint -TenantId "$companyName.onmicrosoft.com" -ClientId $client_id
$owner_user = Get-MgUser -UserId 'owner@$companyEMailDomain'
$member_user = Get-MgUser -UserId "member@$companyEMailDomain"
$filter = "displayName eq '" + $teamName + "'"
$team = Get-MgTeam -Filter "$filter"
(! $team)
{
Write-Host "Creating the new team $teamName..."
$params = @{
"email address removed for privacy reasons" = "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"
visibility = "Private"
displayName = "$teamName"
description = "$teamName Team"
members = @(
@{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
"email address removed for privacy reasons" = "https://graph.microsoft.com/v1.0/users('owner@$companyEMailDomain')"
roles = @(
"owner"
)
}
)
}
New-MgTeam -BodyParameter $params
$team = Get-MgTeam -Filter "$filter"
Write-Host "Completed"
while (! $team)
{
Write-Host "Sleeping 10 seconds..."
Start-Sleep -Seconds 10
$team = Get-MgTeam -Filter "$filter"
}
$params = @{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
roles = @(
"member"
)
"email address removed for privacy reasons" = "https://graph.microsoft.com/v1.0/users('member@$companyEMailDomain')"
}
New-MgTeamMember -TeamId $team.Id -BodyParameter $params
}
else
{
Write-Host "The team '$teamName' already exists...skipping creation!"
}
$url = "https://$companyName.sharepoint.com/sites/" + $teamName.Replace(" ","")
$admin_url = "https://$companyName" + "-admin.sharepoint.com"
try {
Connect-SPOService -Url "$admin_url" -ModernAuth $true -AuthenticationUrl "https://login.microsoftonline.com/{GUID}" -Verbose:$true
Register-SPOHubSite "$url" -Principals $null
}
catch {
Write-Host "Connect-SPOService Failed"
}
$newChannelFilter = "displayName eq 'General'"
$newChannel = Get-MgTeamChannel -TeamId $team.Id -Filter "$newChannelFilter"
$fileFolderUrl = "https://graph.microsoft.com/v1.0/teams/" + $team.Id + "/channels/" + $newChannel.Id + "/filesFolder"
Invoke-MgGraphRequest -Method GET "$fileFolderUrl"
$channels = Get-MgTeamChannel -TeamId $team.Id
foreach ( $department in $departments )
{
$found = $false
foreach ($channel in $channels)
{
if ($channel.DisplayName -eq "$department")
{
$found = $true
}
}
$hubSiteUrl = "https://$companyName.sharepoint.com/sites/" + $teamName.Replace(" ","")
if ($found -eq $false)
{
Write-Host "Creating new channel for $department..."
$params = @{
"@odata.type" = "#Microsoft.Graph.channel"
membershipType = "private"
displayName = "$department"
description = "$department channel"
members = @(
@{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
"email address removed for privacy reasons" = "https://graph.microsoft.com/v1.0/users('owner@$companyEMailDomain')"
roles = @(
"owner"
)
}
@{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
"email address removed for privacy reasons" = "https://graph.microsoft.com/v1.0/users('member@$companyEMailDomain')"
roles = @(
"member"
)
}
)
}
$newChannel = New-MgTeamChannel -TeamId $team.Id -BodyParameter $params
Write-Host "Completed"
}
else
{
Write-Host "The team channel $department already exists...skipping creation!"
}
$channelUrl = "https://$companyName.sharepoint.com/sites/" + $teamName.Replace(" ","") + "-" + $department
$newChannelFilter = "displayName eq '$department'"
$newChannel = Get-MgTeamChannel -TeamId $team.Id -Filter "$newChannelFilter"
$count = 1
try {
$fileFolder = Get-MgTeamChannelFileFolder -TeamId $team.Id -ChannelId $newChannel.Id
}
catch {
}
while (! $fileFolder)
{
Write-Host "Could not find $department channel folder....sleeping 30 seconds and trying again"
Start-Sleep -Seconds 30
try {
$fileFolder = Get-MgTeamChannelFileFolder -TeamId $team.Id -ChannelId $newChannel.Id
}
catch {
}
$count += 1
if ($count -eq 11)
{
Write-Host "ERROR: Could not get the channel site folder...."
break
}
}
if ($fileFolder)
{
Write-Host "Found $department channel folder....adding hub association."
Add-SPOHubSiteAssociation "$channelUrl" -HubSite "$hubSiteUrl"
}
}