Copy documents from OneDrive to SharePoint using Microsoft Graph Explorer

Here, you will learn on how to copy the document from OneDrive to SharePoint library using the Microsoft Graph

Microsoft Graph is a single solution to connect all services across Microsoft Cloud with single authentication. By using this, we can build an application for Microsoft 365 using single API.

Microsoft provides a POSTMAN kind of tool to test the several endpoints from Microsoft Graph call Microsoft Graph Explorer.

This post, walk you through to copy the document from OneDrive to SharePoint library using Microsoft Graph Explorer without any code.

  1. Authenticate with Microsoft Account
  2. Destination Information – Get Folder details from SharePoint
  3. Copy Document – OneDrive to SharePoint

Authentication in Microsoft Graph

First of all, you should have Microsoft business account with a read write access to OneDrive and Share3Point Online site.

  • Navigate to Microsoft Graph Explorer https://developer.microsoft.com/en-us/graph/graph-explorer
  • Login to the Microsoft Graph Explorer with you Microsoft account
  • Ensure your account should have permission to access and modify the files in both OneDrive and SharePoint library.
  • Check the checkboxes to apply the permission scopes ( Files.Read.All, Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All ) in Modify Permissions dialog in Graph Explorer.

We should require the Item Id of the document from Onedrive and Target folder’s unique identifies (driveid and id) from SharePoint Online.

Destination Information – Get Folder details from SharePoint:

To get the drive id and id of the target folder from SharePoint library. we have to follow the below steps,

1. Get SharePoint Site Id

Format:

GET /sites/{host-name}:/{server-relative-path}

Request:

[code lang=”js”]https://graph.microsoft.com/v1.0/sites/ktskumar.sharepoint.com:/sites/dev?$select=id[/code]

Response:

{
“@odata.context”: “https://graph.microsoft.com/v1.0/$metadata#sites(id)/$entity”,
“id”: “ktskumar.sharepoint.com,81fc070d-decb-4dca-8db4-411392dfa157,45e30671-1fb9-4f45-a69a-1227bdfa2c65
}

From the above response, note down the value of id. which is the id of the site, we have to pass in Graph endpoint to get the document library information.

2. Get Document Library Id

Prepare the url as below format to get the document library from the site based on the retrieved site id.

Format:

GET /sites/{site id}/drives?$select=id,name

Request:
[code lang=”js”]https://graph.microsoft.com/v1.0/sites/ktskumar.sharepoint.com,81fc070d-decb-4dca-8db4-411392dfa157,45e30671-1fb9-4f45-a69a-1227bdfa2c65/drives?$select=id,name[/code]

Response:

{
“@odata.context”: “https://graph.microsoft.com/v1.0/$metadata#drives(id,name)”,
“value”: [
{
“id”: “b!DQf8gcveyk2NtEETkt-hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ“,
“name”: “Documents”
}
]
}

From the above response, the value of id represents the id for the SharePoint Document Library “Documents”. We have to pass this id value in Graph endpoint to get the parentReference for the target folder.

3. Get Target Folder’s Driver Id and Id

The combination of both target folder’s driveid and id is used to identify the folder. So, both of the id’s are required in copying the document.
Below is the format for retrieving the sub folders from the root folder of Document library,

Format:

GET /sites/{site id}/drives/{library id}/list/drive/root/children?$filter=folder ne null&$select=name,parentReference

Request:
[code lang=”js”]
https://graph.microsoft.com/v1.0/sites/ktskumar.sharepoint.com,81fc070d-decb-4dca-8db4-411392dfa157,45e30671-1fb9-4f45-a69a-1227bdfa2c65/drives/b!DQf8gcveyk2NtEETkt-hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ/list/drive/root/children?$filter=folder ne null&$select=name,parentReference
[/code]

Response:

{
“@odata.context”: “https://graph.microsoft.com/v1.0/$metadata#sites(‘snips.sharepoint.com%2C81fc070d-decb-4dca-8db4-411392dfa157%2C45e30671-1fb9-4f45-a69a-1227bdfa2c65’)/drives(‘b%21DQf8gcveyk2NtEETkt-hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ’)/list/drive/root/children(name,parentReference)”,
“value”: [
—————
{
“@odata.etag”: “\”{50EBAAC6-C648-4FD6-999D-E96EB05E8C81},1\””,
“name”: “ParentFolder”,
“parentReference”: {
“driveId”: “b!DQf8gcveyk2NtEETkt-hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ“,
“driveType”: “documentLibrary”,
“id”: “01QXE7TVN6Y2GOVW7725BZO354PWSELRRZ“,
“path”: “/drives/b!DQf8gcveyk2NtEETkt-hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ/root:”
}
},
—————–

Note down the values of driveId and id values under parentReference from the response and we have to use these in Request body to define destination folder.

Copy Document: OneDrive to SharePoint

Now we reached the final step of sending the copy request with target headers using Graph Explorer.

The below snippet is the format for copying the document from one place to another place in Office 365.

Format:

POST /me/drive/items/{item-id}/copy

{
“parentReference”: {
“driveId”: <Unique identifier of the drive instance that contains the item>,
“id”: <Unique identifier of the item in the drive>
},
“name”: <target document name>
}

Follow the below steps to complete our copy action,

  • Identify the source document’s item id from OneDrive
  • In the Graph Explorer, select the POST from dropdown
  • Enter the below URL in the textbox next to the method dropdown
    [code lang=”js”]
    https://graph.microsoft.com/v1.0/me/drive/list/items/32/driveItem/copy
    [/code]
  • Enter the below target folder information in Request Body textbox,
    [code lang=”js”]
    {
    "parentReference": {
    "driveId": "b!DQf8gcveyk2NtEETkt-hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ",
    "id": "01QXE7TVN6Y2GOVW7725BZO354PWSELRRZ"
    },
    "name": "Sample Document.docx"
    }
    [/code]
  • Then click on “Run Query” button to start copy the document. After successful run, the document is successfully copied to the SharePoint from OneDrive. We ‘ll get the below output in Graph Explorer,
    MSGraphExplorer: Document Copied Response
    MSGraphExplorer: Document Copied Response
Shantha Kumar
Shantha Kumar
Articles: 280