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.
- Authenticate with Microsoft Account
- Source Information - OneDrive Path and Id Information
- 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=idResponse
- {
- “@odata.context”: “https:
- “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
- {“
- @odata.context”: “https:
- “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 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,parentReferenceResponse
- {“
- @odata.context”: “https:
- “value”: [—————{“
- @odata.etag”: “\”{
- 50E BAAC6 - C648 - 4 FD6 - 999 D - E96EB05E8C81
- },
- 1\””,
- “name”: “ParentFolder”,
- “parentReference”: {“
- driveId”: “b!DQf8gcveyk2NtEETkt - hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ“,
- “driveType”: “documentLibrary”,
- “id”: “01 QXE7TVN6Y2GOVW7725BZO354PWSELRRZ“,
- “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 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,
- {
- "parentReference": {
- "driveId": "b!DQf8gcveyk2NtEETkt-hV3EG40W5H0VPppoSJ736LGUDkfSUnLbiT71N6NwsO3BZ",
- "id": "01QXE7TVN6Y2GOVW7725BZO354PWSELRRZ"
- },
- "name": "Sample Document.docx"
- }
- 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.