使用Microsoft Graph API获取SharePoint文件夹和文档

时间:2019-09-16 19:13:15

标签: sharepoint microsoft-graph microsoft-graph-files

我正在尝试使用Microsoft Graph API从SharePoint文档库中获取文件夹和文档。

如果我对GET提出了https://graph.microsoft.com/v1.0/sites/mysite.sharepoint.com:/sites/MyDocumentSite:/drives/请求,我会找回来的:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives",
    "value":[
        {
            "createdDateTime": "2019-09-05T07:09:49Z",
            "description": "",
            "id": "b!wxh2ZWwoT0KKTdLRYjD5jvzjo8jkat5LgY3VyfgEqkv3YVg_XXXXXXXXXXXXXXXX",
            "lastModifiedDateTime": "2019-09-05T07:09:49Z",
            "name": "Documents",
            "webUrl": "https://mysite.sharepoint.com/sites/MyDocumentSite/Shared%20Documents",
            "driveType": "documentLibrary",
            "createdBy":{"user":{"displayName": "System Account" }},
            "lastModifiedBy":{"user":{"email": "me@myorganization.org", "id": "73f9990c-5c92-4839-8b13-XXXXXXXXXXXX", "displayName": "John Smith"…},
            "quota":{"deleted": 0, "remaining": 0, "total": 0, "used": 0}
        }
    ]
}

但是,如果我尝试通过在ID GET上执行https://graph.microsoft.com/v1.0/sites/mysite.sharepoint.com:/sites/MyDocumentSite:/drives/b!wxh2ZWwoT0KKTdLRYjD5jvzjo8jkat5LgY3VyfgEqkv3YVg_XXXXXXXXXXXXXXXX请求来访问该驱动器,则会收到BadRequest错误:

{
    "error":{
        "code": "BadRequest",
        "message": "Url specified is invalid.",
        "innerError":{
            "request-id": "7c9eaf61-764f-4d72-abdb-ffa2fe868e90",
            "date": "2019-09-16T19:09:41"
        }
    }
}

最终,我想要一种方法来显示文档库中的所有文件夹和文档,但是我似乎无法超越这一初始步骤。

3 个答案:

答案 0 :(得分:1)

获得要查询的驱动器ID后,请在根目录使用/ drives发出请求:

https://graph.microsoft.com/v1.0/drives/<driveId>

答案 1 :(得分:0)

使用“增量” API: https://docs.microsoft.com/en-us/graph/api/driveitem-delta?view=graph-rest-1.0&tabs=http

第一次调用将返回所有项目。 (如果有分页,则需要调用nextLink,直到收到deltaLink为止。)

您可能要使用其中之一 GET / sites / {siteId} / drive / root / delta 要么 GET / drives / {drive-id} / root / delta

答案 2 :(得分:0)

实际上,当站点由site path寻址时,以下查询将失败,但例外:

GET `https://graph.microsoft.com/v1.0/sites/{hostname}:/{server-relative-path}/drive/root/children` 

它似乎是一个 bug ,因为类似的查询但站点为addressed by id时,按预期方式工作:

GET https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root/children

要考虑的另一种选择,是通过其标识符(不指定站点路径或标识符)来获取Drive资源,例如:

GET https://graph.microsoft.com/v1.0/drives/{drive-id}/root/children

轮到获取库中的所有文档和文件夹时,List children of a driveItem endpoint仅返回当前文件夹下的一级。

要返回所有驱动器项目,您至少可以考虑: