Search code examples
microsoft-graph-apionedrive

MS Graph API copy folder of shared folder into the same parent won't copy subfolders and files


Last month I setup a Microsoft Services integration in my angular web app to access the company's personal account onedrive. So we use MSAL as the authentication service and the MSAL_INTERCEPTOR from msal-angular to authenticate our requests. All employees have the company's onedrive as a shared drive. Everything was working fine, but this week I received feedback that the model folder that should be copied with a new name isn't creating the subfolders and files.

Let me show my workflow to copy the model folder in the same parent folder with another name:

  1. Get the model folder's metadata and save the parent info:
GET https://graph.microsoft.com/v1.0/drives/SHARED_DRIVE_ID/items/SHARED_DRIVE_ID!52853:/05-DRM/01-Em%20Andamento/ORC-000_ANO-NOME%20DO%20CONTRATO-GESTOR

response:
{
  ...
  "id": "SHARED_DRIVE_ID!110649",
  "lastModifiedDateTime": "2021-05-21T15:28:55.567Z",
  "name": "ORC-000_ANO-NOME DO CONTRATO-GESTOR",
  "size": 0,
  "webUrl": "https://1drv.ms/f/s!ABM9Mjoi6dy8huA5",
  ...
  "parentReference": {
    "driveId": "SHARED_DRIVE_ID",
    "driveType": "personal",
    "id": "SHARED_DRIVE_ID!95075",
    "name": "01-Em Andamento",
    "path": "/drives/SHARED_DRIVE_ID/items/SHARED_DRIVE_ID!52853:/05-DRM/01-Em%20Andamento"
  },
  "fileSystemInfo": { "createdDateTime": "2021-02-11T13:47:04Z", "lastModifiedDateTime": "2021-03-29T13:50:23.99Z" },
  "folder": {
    "childCount": 8,
    "view": { "viewType": "thumbnails", "sortBy": "takenOrCreatedDateTime", "sortOrder": "ascending" }
  },
  "shared": { "scope": "users", "owner": { "user": { "displayName": "Nortan Diretores", "id": "SHARED_DRIVE_ID" } } }
}

  1. Make the copy request using the "parentReference"
POST https://graph.microsoft.com/v1.0/drives/SHARED_DRIVE_ID/items/SHARED_DRIVE_ID!52853:/05-DRM/01-Em%20Andamento/ORC-000_ANO-NOME%20DO%20CONTRATO-GESTOR:/copy

payload:
{
  "parentReference": { "driveId": "SHARED_DRIVE_ID", "id": "SHARED_DRIVE_ID!95075" },
  "name": "ORC-189_2021-Posto de Combustível 3M-Nichollas Gomes"
}

response:
Status Code: 202 Accepted
  1. The newly copied folder is created with all subfolders and files of the model folder.

  2. After four seconds of the copy request been accepted(Since there is no way to be notified of the copy process), I get the information of the new folder to get the web URL.

This workflow is working in my test on employee personal onedrive folders, and it was working with our shared folder too. But now, in the shared folder, when we reach step 4 of our workflow, only an empty new folder is created.

Trying to understand what is happening, I also noticed that when it made the copy request on the onedrive website is showed a copy operation that fails sometime later with the following error: Error: Try again or refresh page, but I can't debug this error. There is a way to debug this error?

Edit 1: I tried step 2 outside my web app with Postman and the same problem happened.

Edit 2: Request response made with postman:

Staus: 202Accepted    Time: 1596 ms    Size: 719 B

{
  "Date": "Sat, 22 May 2021 16:46:16 GMT",
  "Cache-Control": "no-store",
  "Transfer-Encoding": "chunked",
  "Location": "https://df.api.onedrive.com/v1.0/monitor/4s7kSj8n8-eF7gyNg40BOOKlQvmxjiQ9Yvana5Jl1l87mCOs4AGis4lO95DtmZaccjg-teKUNaY5EQtMya2RnjHfVY7DDbNXgCMYrQju89nzRdbwORmMhSxoy6mds4QfFOmAF92vSB7JL3J2456uxirjwjE3KCkN3KC9TfrJcxvGtFQU1VT7Euk6WMpoq3HeRcOwCtEP1GWuegI3qo1jFSj8PKoyIvTMrZ43snVoiBmjupBHiDlnTUsLkWzqUZ5v1X",
  "Strict-Transport-Security": "max-age=31536000",
  "request-id": "3a02e7bd-dbac-48e6-9a62-939ce0f2e3e0",
  "client-request-id": "3a02e7bd-dbac-48e6-9a62-939ce0f2e3e0",
  "x-ms-ags-diagnostic": {
    "ServerInfo": {
      "DataCenter": "Brazil South",
      "Slice": "E",
      "Ring": "3",
      "ScaleUnit": "000",
      "RoleInstance": "CP1PEPF00001A64"
    }
  }
}

Solution

  • Finally, I figured out what was the problem. The copy endpoint doesn't handle relative paths using an item as a base path like:

    POST https://graph.microsoft.com/v1.0/drives/SHARED_DRIVE_ID/items/SHARED_DRIVE_ID!52853:/05-DRM/01-Em%20Andamento/ORC-000_ANO-NOME%20DO%20CONTRATO-GESTOR:/copy
    

    So, I just changed in my workflow the copy endpoint using the item ID that I got when getting the folder metadata to get the parent info. Now my copy request is:

    POST https://graph.microsoft.com/v1.0/drives/SHARED_DRIVE_ID/items/SHARED_DRIVE_ID!110649/copy
    

    After this change my copy operation monitor result is:

    {
        "errorCode": "",
        "operation": "itemCopy",
        "percentageComplete": 100.0,
        "resourceId": "SHARED_DRIVE_ID!290908",
        "status": "completed",
        "statusDescription": "Completed 10/10 files; 15906/15906 bytes"
    }
    

    Instead,

    {
        "errorCode": "",
        "operation": "itemCopy",
        "percentageComplete": 0.0,
        "status": "notStarted",
        "statusDescription": "Completed 0/0 files; 0/0 bytes"
    }
    

    Thanks @Dev for all your support.