Copy Documents From OneDrive To SharePoint Using Microsoft Graph Explorer

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

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

This post will 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. Source Information - OneDrive Path and Id Information
  3. Target Information – Folder details from SharePoint

Authentication in Microsoft GRAPH

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

  • Navigate to Microsoft Graph Explorer
  • Login to the Microsoft Graph Explorer with your 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 ( Read.All, Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All ) in Modify Permissions dialog in Graph Explorer.

Now, we should require the Item Id of the document from OneDrive and Target folder’s unique identities (drive id 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.

Get SharePoint Site Id

Below is the format for getting the site information and the Request has the URL format for retrieving the Siteid. Response section shows the site information based on our request.

Format

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

Request

https://graph.microsoft.com/v1.0/sites/ktskumar.sharepoint.com:/sites/dev?$select=id

Response

  1. {  
  2.    “@odata.context”: “https://graph.microsoft.com/v1.0/$metadata#sites(id)/$entity”,  
  3.    “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.

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

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

Response

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

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 parent reference for the target folder.

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 ids 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

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

Response 

  1. {“  
  2.     @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)”,  
  3.         “value”: [—————{“  
  4.                 @odata.etag”: “\”{  
  5.                     50E BAAC6 - C648 - 4 FD6 - 999 D - E96EB05E8C81  
  6.                 },  
  7.                 1\””,  
  8.                 “name”: “ParentFolder”,  
  9.                 “parentReference”: {“  
  10.                     driveId”: “b!DQf8gcveyk2NtEETkt - hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ“,  
  11.                     “driveType”: “documentLibrary”,  
  12.                     “id”: “01 QXE7TVN6Y2GOVW7725BZO354PWSELRRZ“,  
  13.                     “path”: “ /drives/b!DQf8gcveyk2NtEETkt - hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ / root: ”  
  14.                 }  
  15.             }, —————– 
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

  1. {  
  2.   "parentReference": {  
  3.     "driveId": <Unique identifier of the drive instance that contains the item>,  
  4.     "id": <Unique identifier of the item in the drive>  
  5.   },  
  6.   "name": <target document name>  

Follow the below steps to complete your 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

    https://graph.microsoft.com/v1.0/me/drive/list/items/32/driveItem/copy
  • Enter the below target folder information in Request Body textbox,
    1. {  
    2.     "parentReference": {  
    3.         "driveId""b!DQf8gcveyk2NtEETkt-hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ",  
    4.         "id""01QXE7TVN6Y2GOVW7725BZO354PWSELRRZ"  
    5.     },  
    6.     "name""Sample Document.docx"  
    7. }  
  • 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.

    SharePoint