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 response indicates creating the channel was a success, but when going to the files tab it shows:
"Your files can’t be found, working on it to restore them."
After having clicked on the files tab in the Teams UI, the folder is created eventually (after a couple of minutes).
But because automated processes depend on the channel folder, we don’t want to ask the user to manually open the team before the other processes can continue.
So: Team created: Ok -> Channel created: Ok-ish (But missing the linked folder) -> One-drive/share point folder, with the name of the channel: Not created
Sometimes the folder is created properly, but lately more often it is not created (Not even after 3-4 days of waiting) until the user opens the files tab in the Teams client.
This behaviour is new since it worked flawlessly until a week ago. Is there a known workaround for this?
- dcc_at_critCopper 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"
}
}
- prannbansalCopper Contributor
sgsmittal I found that if we use delegated permissions instead of application permissions to create the private channel using graph api then the corresponding site is also automatically created
- edwardhangCopper Contributor
- ChowdaryKaruturiCopper Contributor
sgsmittal Did you find a way to create the underlying SP Site without manually clicking on the files tab ? We have a similar situation where we create a bunch of teams/channels programmatically and we dont wanted to click on the files tab manually.
- ChowdaryKaruturiCopper Contributor
Hello sgsmittal, we are having the same problem, is there a way to solve this problem. We would want to programatically create the SharePoint site instead of a user manually clicking on the files.
Thank you,
Chowdary Karuturi
- Abhijit_MSFT
Microsoft
sgsmittal When you create channel using graph api, the current behavior is to visit "Files" and then corresponding SharePoint will be created. let me check.
- Jo_77Copper Contributor
Abhijit_MSFT We're also trying to provision a team through Graph API. Requirement is to create tabs on a few channels that will display documents, but that's not currently possibly as the Folder associated with the Channel does not get created so we can't copy the corresponding documents in to the Team. This happens both when the channel is created via a JSON call to /teams with the team template in the body, and when the channel is created via a call to /teams/{teamId}/channels.
Querying /teams/{teamId}/channels/{newChannelId}/filesFolder after channel creation returns a 404 Not Found error: "Sharepoint folder not found".- sgsmittalCopper Contributor
Jo_77 Abhijit_MSFT I solved it for now by creating folder/driveitem in sharepoint document library after the success api call to create channel
- sgsmittalCopper Contributor
Hi Abhijit_MSFT,
Hope you are doing good
Sure but that will impact the automated process as we have to provision folders as well under file tab in the process (we create team -> then channels -> then folder structure under file tab)
but due to above issue when we try to get files tab Folder using graph api we get error share point folder not found. we can't ask user to visit file tab of each channel as everyday there will lot of team be created automatically with folder structure.
Is there any api using that I can force to create the folder in Sharepoint if that is a current behavior ?
- sgsmittalCopper Contributor
Abhijit_MSFT Did you get a chance to check this ?