简体   繁体   中英

Microsoft Graph API: SharePoint: Get items list in "default Drive" of "root Site"

Overview

I am familiar in using Microsoft Graph API with File scoping for handling items within root directory if (items specifically folders and files ) in Microsoft OneDrive ; create/delete/metadata for Folders and upload/delete/metadata for Files .

Using Microsoft Graph API again, I wish to do perform the same actions in handling Folders and Files, create/delete/metadata for folders and upload/delete/metadata for files , in Microsoft SharePoint within the default Drive of root Site as mention prior for OneDrive .

Problem

I having the following issues with Microsoft Graph API in using for handling items within default Drive or root Site:

  1. Using default Drive ID in handling items within a site's drive.
  2. Create/Delete/Get-Metadata of a folder item within default Site's Drive.
  3. Upload/Delete/Get-Metadata of a file item within a parent folder.

The following Microsoft Graph API call returns root site's default drive 's metadata**:

curl "https://graph.microsoft.com/v1.0/sites/root/drive" \
--request GET \
--verbose \
--write-out 'HTTPSTATUS:%{http_code}' \
--silent \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"

JSON Response example is:

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
  "createdDateTime": "2012-06-12T17:27:56Z",
  "description": "Share a document with the team by adding it to this document library.",
  "id": "b!mWxqgLJ9mESqZI4PrP0Gs-F4hgLaCRlCkVuON4nbwzhkKbcyWdM1Tb5WEzNJ0C60",
  "lastModifiedDateTime": "2015-03-03T02:39:56Z",
  "name": "Shared Documents",
  "webUrl": "https://docusign2com.sharepoint.com/Shared%20Documents",
  "driveType": "documentLibrary",
  "createdBy": {
    "user": {
      "displayName": "System Account"
    }
  },
  "lastModifiedBy": {
    "user": {
      "displayName": "System Account"
    }
  },
  "quota": {
    "deleted": 0,
    "remaining": 0,
    "total": 0,
    "used": 0
  }
}

Problem Getting Drive Metadata

Taking the unencoded drive_id for the current default Drive in root Site, [** DRIVE_ID **] :

"id": "b!mWxqgLJ9mESqZI4PrP0Gs-F4hgLaCRlCkVuON4nbwzhkKbcyWdM1Tb5WEzNJ0C60"

Encode drive_id , [** URL-Encoded DRIVE_ID **] :

"id": "b%21mWxqgLJ9mESqZI4PrP0Gs-F4hgLaCRlCkVuON4nbwzhkKbcyWdM1Tb5WEzNJ0C60"

Using the encoded drive_id , we can get the metadata for that drive another way:

curl "https://graph.microsoft.com/v1.0/sites/root/drives/[** URL-Encoded DRIVE_ID **]" \
--request GET \
--verbose \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"

Response:

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
  "createdDateTime": "2012-06-12T17:27:56Z",
  "description": "Share a document with the team by adding it to this document library.",
  "id": "[** DRIVE_ID **]",
  "lastModifiedDateTime": "2015-03-03T02:39:56Z",
  "name": "Shared Documents",
  "webUrl": "https://docusign2com.sharepoint.com/Shared%20Documents",
  "driveType": "documentLibrary",
  "createdBy": {
    "user": {
      "displayName": "System Account"
    }
  },
  "lastModifiedBy": {
    "user": {
      "displayName": "System Account"
    }
  },
  "quota": {
    "deleted": 0,
    "remaining": 0,
    "total": 0,
    "used": 0
  }
}

Next, I tried listing the items within the root Site's default Drive by appending /items :

curl "https://graph.microsoft.com/v1.0/sites/root/drives/[** URL-Encoded DRIVE_ID **]/items" \
--request GET \
--verbose \
--header "authorization: Bearer [** ACCESS_TOKEN **" \
--header "Content-Type: application/json"

And it fails:

{
  "error": {
    "code": "invalidRequest",
    "message": "The request is malformed or incorrect.",
    "innerError": {
      "request-id": "0a212014-b386-45d9-9c36-bae2dd6cea8f",
      "date": "2020-02-07T06:51:15"
    }
  }
}

API Path Requested

What is the expected path for getting the list of all items within a default drive's root within root site?

Thank you

Instead of using .../v1.0/sites/$site_id/drives/... for working with drives, I switched to using .../v1.0/drives/$drive_id/... , and this worked.

The following Microsoft Graph API request above using /v1.0/sites/root/drives/[** URL-Encoded DRIVE_ID **] , the return previously acquired default drive (identified by [** DRIVE_ID **] ) metadata:

curl "https://graph.microsoft.com/v1.0/drives/[** url-encoded DRIVE_ID **]" \
--request GET \
--verbose \
--silent \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
  "createdDateTime": "2012-06-12T17:27:56Z",
  "description": "Share a document with the team by adding it to this document library.",
  "id": "[** DRIVE_ID **]",
  "lastModifiedDateTime": "2015-03-03T02:39:56Z",
  "name": "Shared Documents",
  "webUrl": "https://docusign2com.sharepoint.com/Shared%20Documents",
  "driveType": "documentLibrary",
  "createdBy": {
    "user": {
      "displayName": "System Account"
    }
  },
  "lastModifiedBy": {
    "user": {
      "displayName": "System Account"
    }
  },
  "quota": {
    "deleted": 0,
    "remaining": 0,
    "total": 0,
    "used": 0
  }
}

This returns the root folder of default drive:

curl "https://graph.microsoft.com/v1.0/drives/[** url-encoded DRIVE_ID **]/root" \
--request GET \
--verbose \
--silent \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('[** url-encoded DRIVE_ID **]')/root/$entity",
  "createdDateTime": "2012-06-12T17:27:56Z",
  "id": "[** ROOT_FOLDER_ID **]",
  "lastModifiedDateTime": "2020-02-07T16:04:09Z",
  "name": "root",
  "webUrl": "https://docusign2com.sharepoint.com/Shared%20Documents",
  "size": 27781340,
  "parentReference": {
    "driveId": "b!mWxqgLJ9mESqZI4PrP0Gs-F4hgLaCRlCkVuON4nbwzhkKbcyWdM1Tb5WEzNJ0C60",
    "driveType": "documentLibrary"
  },
  "fileSystemInfo": {
    "createdDateTime": "2012-06-12T17:27:56Z",
    "lastModifiedDateTime": "2020-02-07T16:04:09Z"
  },
  "folder": {
    "childCount": 5
  },
  "root": {}
}

Now with root folder of default drive, the list of children folders can be requested:

curl "https://graph.microsoft.com/v1.0/drives/[** url-encoded DRIVE_ID **]/items/[** ROOT_FOLDER_ID **]/children" \
--request GET \
--verbose \
--silent \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM