'File Upload to Channel via Microsoft Teams Graph Api
I have been trying to do a file upload to a channel using the graph Api. I can successful do an upload to my personal OneDrive, using the request https://graph.microsoft.com:443/v1.0/me/drive/root:/fileName:/content
However I am unable to upload to a channel. I have tried many combinations, but keep getting invalidRequest. Below are some of the request I have tried:
I have also tried inputting the channel ID in various ways:
xx:[email protected] [email protected] xx:xxxxxxxxxxxxxxxxxxxxxxxxx
Solution 1:[1]
I found out what I was doing wrong. For anybody else who is having an issue, you have to get the folder id that backs the channel. Here is an example in C#.
// Get the folder of the channel (driveItem)
var item = await graphClient.Teams[teamID].Channels[channelID].FilesFolder
.Request().GetAsync();
// Do the put request
var uploadRequest = graphClient
//.Me.Drive.Root
.Groups[teamID]
.Drive
.Items[item.Id]
.ItemWithPath(file.FileName)
.Content.Request()
.PutAsync<DriveItem>(file.OpenReadStream());
Solution 2:[2]
I spent hours with this topic. Your solution wasn't working for me but I finally got it done. To save others I'd like to share my solution too:
I use this to get the drive item for the channel:
var request = graphClient.Teams[teamId].Channels[channelId].FilesFolder.Request().GetAsync();
And then upload with:
var uploadRequest = graphClient.Drives[driveItem.ParentReference.DriveId].Items[driveItem.Id].ItemWithPath("testfile.pdf").Content.Request()
.PutAsync<DriveItem>(System.IO.File.OpenRead(filePath));
My application authenticates with a client secret. Maybe this makes a difference.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | RockLeeroy |
Solution 2 | sradforth |