I'm trying to automate SharePoint tasks from Microsoft Access using the REST API (since the CSOM is not available in VBA). This, however, requires me to authenticate.
I usually authenticate to SharePoint using Windows Authentication. I'm not required to enter a password to access OneDrive for Business or when opening files from SharePoint in Word.
The following code returns 403 Unauthorized and a System.UnauthorizedAccessException in the body of the response:
Dim xmlhttp As New MSXML2.XMLHTTP60
xmlhttp.Open "GET", "https://mycompany.sharepoint.com/mysite/_api/web", False
xmlhttp.setRequestHeader "Accept", "application/json;odata=verbose"
xmlhttp.send 'Executes a REST api request
Debug.Print xmlhttp.Status; xmlhttp.statusText 'Print status
Debug.Print xmlhttp.responseText 'Print the result
However, the following seems to work (returns 200 OK and the expected body):
DoCmd.TransferSharePointList acLinkSharePointList, https://mycompany.sharepoint.com/mysite, "UserInfo" 'Create a linked list, userinfo is present by default
DoCmd.DeleteObject acTable, "UserInfo" 'Delete the list you've just created
'From here, code is identical to what's shared above
Dim xmlhttp As New MSXML2.XMLHTTP60
xmlhttp.Open "GET", "https://mycompany.sharepoint.com/mysite/_api/web", False
xmlhttp.setRequestHeader "Accept", "application/json;odata=verbose"
xmlhttp.send 'Sends a REST api request
Debug.Print xmlhttp.Status; xmlhttp.statusText 'Print status
Debug.Print xmlhttp.responseText 'Print the result
Moreover, after creating and deleting a list, all my XMLHTTP requests seem to get authenticated automagically throughout the lifetime of the Access Application object (I don't need to create and delete the list over and over again). I'm not doing anything with request headers or cookies.
Can anyone explain to me why that is?
Is it documented somewhere?
Is there an alternate, better way to connect using VBA to modify files?