Many enterprise companies have legacy applications that were built using VB, VBA, VB6, or VB.net. These applications are critical for their business operations and cannot simply be abandoned or replaced. However, as technology continues to evolve, it becomes increasingly difficult to keep these legacy applications up to date with the latest APIs and services.
Fortunately, Microsoft Graph provides a unified API for accessing data and resources across various Microsoft services, including Office 365, Azure Active Directory, OneDrive, and SharePoint. This makes it easier for developers to build modern applications that integrate with these services.
However, integrating with Microsoft Graph can be challenging for legacy VB applications. This is where the Chilkat Library can help.
The Chilkat Library is a collection of libraries for various programming languages, including VB6 and VB.net. It provides a set of tools and components for working with various protocols, including HTTP, FTP, SMTP, POP3, and more. It also includes components for working with encryption, compression, and digital signatures.
By using the Chilkat Library, developers can easily integrate with Microsoft Graph and modernize their legacy VB applications. For example, they can use the Chilkat HTTP component to make HTTP requests to the Microsoft Graph API and retrieve data from Office 365, OneDrive, or SharePoint. They can also use the Chilkat FTP component to upload or download files to or from OneDrive.
Here are a few examples of using the Chilkat HTTP component to retrieve or update files in a SharePoint document using Azure AD application permissions (Client Id and a secret):
Module MicrosoftGraphModule
Sub Main()
Dim http As New Chilkat.Http
http.SessionLogFilename = "qa_output/sessionLogB.txt"
''----------------------------------- Authenticate Microsoft Graph ----------------------
Dim req1 As New Chilkat.HttpRequest
req1.HttpVerb = "POST"
req1.ContentType = "application/x-www-form-urlencoded"
req1.AddParam("grant_type", "client_credentials")
req1.AddParam("scope", "https://graph.microsoft.com/.default")
req1.AddParam("client_id", "7bc61979-...")
req1.AddParam("client_secret", "Brx8Q...")
Dim resp1 As Chilkat.HttpResponse = http.PostUrlEncoded("https://login.microsoftonline.com/<your-tenant-name>.onmicrosoft.com/oauth2/v2.0/token", req1)
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Debug.WriteLine("Response body:")
Debug.WriteLine(resp1.BodyStr)
Dim responseJson As New Chilkat.JsonObject
resp1.GetBodyJson(responseJson)
Dim accessToken As String
accessToken = responseJson.StringOf("access_token")
Debug.WriteLine(accessToken)
''----------------------------------- Add token to the http client ----------------------
http.SetRequestHeader("Authorization", "Bearer " + accessToken)
''----------------------------------- Get site details ----------------------
Dim siteRelativeUrl = "/sites/workspace"
Dim resp2 As Chilkat.HttpResponse = http.QuickGetObj("https://graph.microsoft.com/v1.0/sites/root:" + siteRelativeUrl)
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Debug.WriteLine(resp2.BodyStr)
'Response Data
'{
' "@odata.context":"https://graph.microsoft.com/v1.0/$metadata#sites/$entity",
' "createdDateTime":"2023-01-26T11:21:10.157Z","description":"",
' "id":"y2zh0.sharepoint.com,806c2c3f-301e-48e4-aabf-8a875dd8f435,2cebccfa-00e7-4c92-a8c6-0734a972177d",
' "lastModifiedDateTime":"2023-03-20T10:03:12Z",
' "name":"Workspace",
' "webUrl":"https://y2zh0.sharepoint.com/sites/Workspace",
' "displayName":"Workspace Code - Workspace Name",
' "root":{},
' "siteCollection":{"hostname":"y2zh0.sharepoint.com"}
' }
Dim responseJson2 As New Chilkat.JsonObject
resp2.GetBodyJson(responseJson2)
Dim siteId As String
siteId = responseJson2.StringOf("id")
''----------------------------------- Get site default 'Documents' Library ----------------------
Dim resp3 As Chilkat.HttpResponse = http.QuickGetObj("https://graph.microsoft.com/v1.0/sites/root:" + siteRelativeUrl + ":/drive")
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Debug.WriteLine(resp3.BodyStr)
'Response Data
'{
' "@odata.context""https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
' "createdDateTime":"2022-10-23T05:05:39Z","description":"",
' "id":"b!PyxsgB4w5Eiqv4qHXdj0NfrM6yznAJJMqMYHNKlyF32sxhJ6N60zTL1dj3vGBXfC",
' "lastModifiedDateTime":"2023-02-16T10:54:56Z",
' "name":"Documents",
' "webUrl":"https://y2zh0.sharepoint.com/sites/Workspace/Shared%20Documents",
' "driveType":"documentLibrary",
' "createdBy":{"user":{"displayName":"System Account"}},
' "lastModifiedBy":{"user":{"email":"velin@y2zh0.onmicrosoft.com","id":"3c231164-747b-46b2-a438-5ecacf83f6b0","displayName":"Velin Georgiev"}},
' "owner":{"user":{"email":"velin@y2zh0.onmicrosoft.com","id":"3c231164-747b-46b2-a438-5ecacf83f6b0","displayName":"Velin Georgiev"}},
' "quota":{"deleted":0,"remaining":27487788386432,"state":"normal","total":27487790694400,"used":2307968}
' }
Dim responseJson3 As New Chilkat.JsonObject
resp3.GetBodyJson(responseJson3)
Dim driveId As String
driveId = responseJson3.StringOf("id")
Debug.WriteLine(driveId)
''----------------------------------- 'List Documents in default 'Documents' Library ----------------------
Dim resp4 As Chilkat.HttpResponse = http.QuickGetObj("https://graph.microsoft.com/v1.0/drives/" + driveId + "/items/root/children")
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Debug.WriteLine(resp4.BodyStr)
''----------------------------------- 'Search for Document in Document Library ----------------------
Dim searchTerm = "Document.docx"
Dim resp5 As Chilkat.HttpResponse = http.QuickGetObj("https://graph.microsoft.com/v1.0/drives/" + driveId + "/root/search(q='" + searchTerm + "')")
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Dim responseJson5 As New Chilkat.JsonObject
resp5.GetBodyJson(responseJson5)
' Response Data
'{{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)","value":[{"@odata.type":"#microsoft.graph.driveItem","createdDateTime":"2023-01-26T11:47:25Z","id":"01JPBGVWUWC6WOID5FAZHKKSWZHEBB5OIL","lastModifiedDateTime":"2023-01-26T11:49:02Z","name":"Document.docx","webUrl":"https://y2zh0.sharepoint.com/sites/Workspace/_layouts/15/Doc.aspx?sourcedoc=%7BE4AC1796-A50F-4E06-A54A-D939021EB90B%7D&file=Document.docx&action=default&mobileredirect=true&DefaultItemOpen=1","size":19639,"createdBy":{"user":{"email":"velin@y2zh0.onmicrosoft.com","displayName":"Velin Georgiev"}},"lastModifiedBy":{"user":{"email":"velin@y2zh0.onmicrosoft.com","displayName":"Velin Georgiev"}},"parentReference":{"driveType":"documentLibrary","driveId":"b!PyxsgB4w5Eiqv4qHXdj0NfrM6yznAJJMqMYHNKlyF32sxhJ6N60zTL1dj3vGBXfC","id":"01JPBGVWV6Y2GOVW7725BZO354PWSELRRZ"},"file":{"mimeType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document"},"fileSystemInfo":{"createdDateTime":"2023-01-26T11:47:25Z","lastModifiedDateTime":"2023-01-26T11:49:02Z"},"searchResult":{},"shared":{"scope":"users"}}]}}
Dim items = responseJson5.ArrayOf("value")
Dim firstItem = items.ObjectAt(0)
Dim itemId = firstItem.StringOf("id")
''----------------------------------- 'Find Document by Id ----------------------
Dim resp6 As Chilkat.HttpResponse = http.QuickGetObj("https://graph.microsoft.com/v1.0/drives/" + driveId + "/items/" + itemId)
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Debug.WriteLine(resp6.BodyStr)
''----------------------------------- 'Download Document by Id ----------------------
http.Download("https://graph.microsoft.com/v1.0/drives/" + driveId + "/items/" + itemId + "/content", "C:\Users\velin.georgiev\source\repos\ConsoleApp1\word.docx")
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
''----------------------------------- 'Upload Document ----------------------
Dim filepath As String
filepath = "C:\Users\velin.georgiev\source\repos\ConsoleApp1\ConsoleApp1\image.png"
Dim req As New Chilkat.HttpRequest
req.HttpVerb = "PUT"
req.Path = "/v1.0/drives/" + driveId + "/items/root:/image.png:/content"
' The below path also works
'req.Path = "/v1.0/sites/" + siteId + "/drives/" + driveId + "/root:/image.png:/content"
req.AddHeader("Authorization", "Bearer " + accessToken)
req.AddHeader("accept", "application/json;odata.metadata=none")
req.StreamBodyFromFile(filepath)
' Do the upload using https (TLS). Port 443 is the default port for https.
Dim bUseTls As Long
bUseTls = 1
Dim resp8 As Chilkat.HttpResponse = http.SynchronousRequest("graph.microsoft.com", 443, bUseTls, req)
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Debug.WriteLine(resp8.BodyStr)
''----------------------------------- 'Get Metadata of a Document ----------------------
Dim resp9 As Chilkat.HttpResponse = http.QuickGetObj("https://graph.microsoft.com/v1.0/drives/" + driveId + "/items/" + itemId)
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Dim responseJson9 As New Chilkat.JsonObject
resp9.GetBodyJson(responseJson9)
Dim itemId9 = responseJson9.StringOf("id")
Debug.WriteLine(itemId9)
''----------------------------------- 'Update Metadata of a Document ----------------------
Dim req10 As New Chilkat.HttpRequest
req10.HttpVerb = "PATCH"
req10.Path = "/v1.0/drives/" + driveId + "/items/" + itemId
req10.AddHeader("Authorization", "Bearer " + accessToken)
req10.AddHeader("accept", "application/json;odata.metadata=minimal")
req10.AddHeader("Content-Type", "application/json")
' Specify the properties you'd like to update
req10.LoadBodyFromString("{""name"":""Title Test 123.docx""}", "utf-8")
Dim res10 As Chilkat.HttpResponse = http.SynchronousRequest("graph.microsoft.com", 443, 1, req10)
If (http.LastMethodSuccess = False) Then
Debug.WriteLine(http.LastErrorText)
Exit Sub
End If
Debug.WriteLine(res10.BodyStr)
End Sub
End Module
Using the Chilkat Library, developers can easily integrate their legacy VB applications with Microsoft Graph and modernize their applications without having to completely rewrite them. This can save time, effort, and resources, and provide a more seamless experience for end-users.