Create folder inside channel of a Teams' team using Graph from VBA

Inácio Rodrigues 0 Reputation points
2024-03-28T15:33:54.5866667+00:00

Hello,

I'm trying to create a folder on a team's channel in Teams, using the Graph API inside VBA running in an Excel file. Basically, I have an excel file with data and a button, and when the button is pushed, it would trigger the creation of a folder inside a channel of a team. For this, I have registered an app and provide it with appropriate permissions and consent. I'm successful at retrieving an access token for the app, but the POST fails with error 500 (InternalServerError - Unable to find target address). Slight changes to the url (filesFolder » filesFolder:)may result in Status code 400. Here is the relevant code I'm using:

' Get access token using the fetched values

Dim accessToken As String

accessToken = GetAccessToken(clientId, clientSecret, tenantId)

Debug.Print "The access token is: " & accessToken

Dim url As String

Dim jsonBody As String

Dim xmlHttp As Object

Dim teamId As String

Dim channelId As String

teamId = Range("AS_QMS_TEAM_ID").Value

channelId = Range("AS_PCM_CHANNEL_ID").Value

' Construct the URL for the Graph API endpoint

url = "https://graph.microsoft.com/v1.0/teams/" & teamId & "/channels/" & channelId & "/filesFolder/children"

' The JSON body of the request

jsonBody = "{""name"": ""DRFQ-001"", ""folder"": {}}"

' Create an XMLHTTP object

Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP")

' Open the request and set headers

xmlHttp.Open "POST", url, False

xmlHttp.setRequestHeader "Content-Type", "application/json"

xmlHttp.setRequestHeader "Authorization", "Bearer " & accessToken

' Send the request

xmlHttp.Send jsonBody

' Print status and response

Debug.Print "Status: " & xmlHttp.Status

Debug.Print "Response: " & xmlHttp.responseText

Actually, the end idea is to create a folder inside an existing folder but for now I would be happy to create it on the root folder of the channel... The problem is most probably on the request url but I'm too rookie on Graph to find it.

Any idea?

Thank you!

Inácio

Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,077 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,594 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,630 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,572 questions
{count} votes