
First, make sure you have the following NuGet packages installed in your project:
Microsoft.Graph
Microsoft.Identity.Client
Then, you can use the following code to connect to Word Online:
Imports Microsoft.Graph
Imports Microsoft.Identity.Client
Module Module1
Sub Main()
' Set the Graph API endpoint for Word Online
Const wordOnlineEndpoint As String = "https://graph.microsoft.com/v1.0/me/drive/root:/Documents"
' Set the Azure AD app registration information
Const appId As String = "<Your app ID goes here>"
Const redirectUri As String = "http://localhost"
Const tenantId As String = "<Your tenant ID goes here>"
' Create a PublicClientApplication object with the app registration information
Dim pca As New PublicClientApplication(appId, $"https://login.microsoftonline.com/{tenantId}")
' Create a new GraphServiceClient object with an authentication provider that uses MSAL to get an access token
Dim graphClient As New GraphServiceClient(New DelegateAuthenticationProvider(
Async Function(requestMessage)
Dim result = Await pca.AcquireTokenInteractive({"Files.ReadWrite"}) _
.ExecuteAsync()
requestMessage.Headers.Authorization =
New System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken)
End Function))
' Get the list of files in the user's Word Online root folder
Dim files = Await graphClient.Me.Drive.Root.ItemWithPath("Documents").Children.Request().GetAsync()
' Print the name of each file
For Each file In files
Console.WriteLine(file.Name)
Next
End Sub
End Module
In this example, we're using the GraphServiceClient class from the Microsoft.Graph namespace to make requests to the Microsoft Graph API. We're also using the PublicClientApplication class from the Microsoft.Identity.Client namespace to authenticate the user and get an access token for the API.
To use this code in your own application, you'll need to replace the appId and tenantId constants with your own Azure AD app registration information, and you may need to modify the wordOnlineEndpoint constant to point to a different location in the user's OneDrive for Business.