Create Sharepoint folder from VBA (access 2016)

℡╲_俬逩灬. 提交于 2019-12-11 16:46:43

问题


I have an access 2016 app that currently creates a folder on a shared drive related to the DB record the user is working on.

We are migrating to SP online and I need to re-create something similar

I can list the files and folders currently in a directory using the API but when I try and create a new folder I am getting an error

"{"error":{"code":"-2130575251, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."}}}"

The only thing I can see I am doing differently is I am not including a Authorization: "Bearer " + accessToken header in the post.

I haven't been including it in the gets for the Files list and it is working using the cached credentials from IE.

Getting the file and folder like this is working


    'create XML HTTP object
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

    With objXMLHTTP

        'open connection to site
        .Open "GET", url, False
        '.send

        .setRequestHeader "Content-Type", "application/json; charset=UTF-8"
        .setRequestHeader "Accept", "application/json"
        .send

        Do Until .ReadyState = 4: DoEvents: Loop

        sJSONString = .responseText

    End With


    Dim Json As Object
    Set Json = JsonConverter.ParseJson(sJSONString)
    Debug.Print JsonConverter.ConvertToJson(Json, Whitespace:=2)

    Dim value As Dictionary

    For Each value In Json("value")
        Debug.Print "> " + value("Name")
    Next value

However trying to create a folder fails


    strPostData = "{ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/Shared Documents/Folder1'}"

    'Set objXMLHTTP = New MSXML2.XMLHTTP
    With objXMLHTTP
        .Open "POST", url, False
        .setRequestHeader "accept", "application/json;odata=verbose"
        .setRequestHeader "Content-Type", "application/json;odata=verbose"
        .send strPostData
        strResponse = .responseText
    End With

Can anyone advise if what I am trying is possible and if so what I am doing is wrong?


回答1:


Sorry, almost immediately I found and answer!

Needed to add

        .setRequestHeader "X-RequestDigest", digest
        .setRequestHeader "Content-Length", Len(strPostData)

and it all started working!

Thanks anyway, hope this helps someone else



来源:https://stackoverflow.com/questions/58243289/create-sharepoint-folder-from-vba-access-2016

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!