💻

Microsoft Graph の List API と Drive API を相互変換する

2022/01/07に公開

はじめに

Microsoft Graph の Drive API では SharePoint のサイト構造を意識することなく、SharePoint や OneDrive for Business のファイルを扱うことができます。しかし Drive API の DriveDriveItem の ID は SharePoint REST API の ListListItem の ID と互換性がないため、時として不便に感じることがあります。DriveDriveItem の ID をもとに ListListItem を取得する、または ListListItem の ID をもとに DriveDriveItem を取得する方法について確認したいと思います。

Drive から List を取得する

以下の API を使って Drive から List を取得することができます。

https://graph.microsoft.com/v1.0/drives/{{drive-id}}/list

https://docs.microsoft.com/ja-jp/graph/api/resources/drive?WT.mc_id=M365-MVP-5002941

Graph Explorer のサンプル データで実行してみると以下のような感じです。

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('b%21CbtYWrofwUGBJWnaJkNwoNrBLp_kC3RKklSXPwrdeP3yH8_qmH9xT5Y6RODPNfYI')/list/$entity",
  "@odata.etag": "\"eacf1ff2-7f98-4f71-963a-44e0cf35f608,203\"",
  "createdDateTime": "2017-07-27T02:32:33Z",
  "description": "",
  "eTag": "\"eacf1ff2-7f98-4f71-963a-44e0cf35f608,203\"",
  "id": "eacf1ff2-7f98-4f71-963a-44e0cf35f608",
  "lastModifiedDateTime": "2022-01-04T05:30:38Z",
  "name": "Shared Documents",
  "webUrl": "https://m365x214355.sharepoint.com/Shared%20Documents",
  "displayName": "Documents",
  "createdBy": {
      "user": {
          "displayName": "System Account"
      }
  },
  "lastModifiedBy": {
      "user": {
          "displayName": "System Account"
      }
  },
  "parentReference": {
      "siteId": "m365x214355.sharepoint.com,5a58bb09-1fba-41c1-8125-69da264370a0,9f2ec1da-0be4-4a74-9254-973f0add78fd"
  },
  "list": {
      "contentTypesEnabled": false,
      "hidden": false,
      "template": "documentLibrary"
  }
}

List から Drive を取得する

以下の API を使って List から Drive を取得することができます。

https://graph.microsoft.com/v1.0/sites/{{site-id}}/lists/{{list-id}}/drive

https://docs.microsoft.com/ja-jp/graph/api/resources/list?WT.mc_id=M365-MVP-5002941

Graph Explorer のサンプル データで実行してみると以下のような感じです。

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
  "createdDateTime": "2017-07-27T02:32:33Z",
  "description": "",
  "id": "b!CbtYWrofwUGBJWnaJkNwoNrBLp_kC3RKklSXPwrdeP3yH8_qmH9xT5Y6RODPNfYI",
  "lastModifiedDateTime": "2021-10-02T09:26:38Z",
  "name": "Documents",
  "webUrl": "https://m365x214355.sharepoint.com/Shared%20Documents",
  "driveType": "documentLibrary",
  "createdBy": {
      "user": {
          "displayName": "System Account"
      }
  },
  "lastModifiedBy": {
      "user": {
          "displayName": "System Account"
      }
  },
  "owner": {
      "group": {
          "id": "43a63cc2-582b-4d81-a79d-1591f91d5558",
          "displayName": "Company Administrator"
      }
  },
  "quota": {
      "deleted": 0,
      "remaining": 27486887980470,
      "state": "normal",
      "total": 27487790694400,
      "used": 902713930
  }
}

DriveItem から ListItem を取得する

以下の API を使って DriveItem から ListItem を取得することができます。

https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/{{drive-item-id}}/listItem

https://docs.microsoft.com/ja-jp/graph/api/resources/driveitem?WT.mc_id=M365-MVP-5002941

Graph Explorer のサンプル データで実行してみると以下のような感じです。

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('b%21CbtYWrofwUGBJWnaJkNwoNrBLp_kC3RKklSXPwrdeP3yH8_qmH9xT5Y6RODPNfYI')/items('01OXYKUGW6HG5WM2OBTVDZ72ABJAY5P4BY')/listItem/$entity",
  "@odata.etag": "\"66bb39de-c169-479d-9fe8-014831d7f038,1\"",
  "createdDateTime": "2017-08-06T01:18:34Z",
  "eTag": "\"66bb39de-c169-479d-9fe8-014831d7f038,1\"",
  "id": "45",
  "lastModifiedDateTime": "2017-08-06T01:18:34Z",
  "webUrl": "https://m365x214355.sharepoint.com/Shared%20Documents/DemoDocs/AdminDemo",
  "createdBy": {
    "user": {
      "email": "provisioninguser1@m365x214355.onmicrosoft.com",
      "displayName": "Provisioning User"
    }
  },
  "lastModifiedBy": {
    "user": {
      "email": "provisioninguser1@m365x214355.onmicrosoft.com",
      "displayName": "Provisioning User"
    }
  },
  "parentReference": {
    "id": "870e69b3-b053-4d4c-a898-91d26fd74e63",
    "siteId": "m365x214355.sharepoint.com,5a58bb09-1fba-41c1-8125-69da264370a0,9f2ec1da-0be4-4a74-9254-973f0add78fd"
  },
  "contentType": {
    "id": "0x012000A0DEBD25FF52B745937EF93199BFD040",
    "name": "Folder"
  },
  "fields@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('b%21CbtYWrofwUGBJWnaJkNwoNrBLp_kC3RKklSXPwrdeP3yH8_qmH9xT5Y6RODPNfYI')/items('01OXYKUGW6HG5WM2OBTVDZ72ABJAY5P4BY')/listItem/fields/$entity",
  "fields": {
    "@odata.etag": "\"66bb39de-c169-479d-9fe8-014831d7f038,1\"",
    "FileLeafRef": "AdminDemo",
    "id": "45",
    "ContentType": "Folder",
    "Created": "2017-08-06T01:18:34Z",
    "AuthorLookupId": "36",
    "Modified": "2017-08-06T01:18:34Z",
    "EditorLookupId": "36",
    "_CheckinComment": "",
    "LinkFilenameNoMenu": "AdminDemo",
    "LinkFilename": "AdminDemo",
    "ItemChildCount": "4",
    "FolderChildCount": "0",
    "_ComplianceFlags": "",
    "_ComplianceTag": "",
    "_ComplianceTagWrittenTime": "",
    "_ComplianceTagUserId": "",
    "_CommentCount": "",
    "_LikeCount": "",
    "_DisplayName": "",
    "Edit": "0",
    "_UIVersionString": "1.0",
    "ParentVersionStringLookupId": "45",
    "ParentLeafNameLookupId": "45"
  }
}

ListItem から DriveItem を取得する

以下の API を使って ListItem から DriveItem を取得することができます。

https://graph.microsoft.com/v1.0/sites/{{site-id}}/lists/{{list-id}}/items/{{list-item-id}}/driveItem

https://docs.microsoft.com/ja-jp/graph/api/resources/listitem?WT.mc_id=M365-MVP-5002941

Graph Explorer のサンプル データで実行してみると以下のような感じです。

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('m365x214355.sharepoint.com%2C5a58bb09-1fba-41c1-8125-69da264370a0%2C9f2ec1da-0be4-4a74-9254-973f0add78fd')/lists('eacf1ff2-7f98-4f71-963a-44e0cf35f608')/items('45')/driveItem/$entity",
  "createdDateTime": "2017-08-06T01:18:34Z",
  "eTag": "\"{66BB39DE-C169-479D-9FE8-014831D7F038},1\"",
  "id": "01OXYKUGW6HG5WM2OBTVDZ72ABJAY5P4BY",
  "lastModifiedDateTime": "2017-08-06T01:18:34Z",
  "name": "AdminDemo",
  "webUrl": "https://m365x214355.sharepoint.com/Shared%20Documents/DemoDocs/AdminDemo",
  "cTag": "\"c:{66BB39DE-C169-479D-9FE8-014831D7F038},0\"",
  "size": 9921,
  "createdBy": {
      "user": {
          "displayName": "Provisioning User"
      }
  },
  "lastModifiedBy": {
      "user": {
          "displayName": "Provisioning User"
      }
  },
  "parentReference": {
      "driveId": "b!CbtYWrofwUGBJWnaJkNwoNrBLp_kC3RKklSXPwrdeP3yH8_qmH9xT5Y6RODPNfYI",
      "driveType": "documentLibrary",
      "id": "01OXYKUGVTNEHIOU5QJRG2RGER2JX5OTTD",
      "path": "/drives/b!CbtYWrofwUGBJWnaJkNwoNrBLp_kC3RKklSXPwrdeP3yH8_qmH9xT5Y6RODPNfYI/root:/DemoDocs"
  },
  "fileSystemInfo": {
      "createdDateTime": "2017-08-06T01:18:34Z",
      "lastModifiedDateTime": "2017-08-06T01:18:34Z"
  },
  "folder": {
      "childCount": 4
  }
}

おわりに

Microsoft Docs に書いてあるそのままなのですがなかなか忘れそうなので。ちなみに Microsoft Graph におけるサイトの ID は {{domain-name}},{{site-id}},{{web-id}} の形式になります。SharePoint REST API の ID がそのまま使えるわけではないのでお気を付けください。

Discussion