Search code examples
microsoft-graph-apionedrive

Uploading items to a personal OneDrive shared folder


OneDrive user A shares a folder with OneDrive user B, and B can access that folder by using the share id. For example using the graph explorer

GET https://graph.microsoft.com/v1.0/shares/{shareId}

yields

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#shares/$entity",
    "id": "{shareId}",
    "name": "ASharedFolder",
    "owner": { ... }
}

Now, B wants to upload a new file to ASharedFolder.

Reading the OneDrive docs for upload I have tried

PUT https://graph.microsoft.com/v1.0/shares/{shareId}/driveItem/children:/SomeFile.txt:/content
Content-Type text/plain
some text goes here

as well as

PUT https://graph.microsoft.com/v1.0/shares/{shareId}/items/{sharedItemId}:/SomeFile.txt:/content
Content-Type text/plain
some text goes here

but both yield "BadRequest", "Unsupported segment type..."

Edit: I have now played out this scenario in the OneDrive Web UI using two different browsers for OneDrive users A and B, so I know that it is possible (without first adding the shared folder to B's own root), but I need some help figuring out the right request for the OneDrive REST API.

Does anyone know?


Solution

  • I checked the possibility of uploading files to a shared folder on OneDrive belonging to another user. I did not have any problems to achieve it using GraphExplorer.

    Here is what I did:

    1. I got a list of shared files and folders:

    GET /me/drive/sharedWithMe

    Returned (some of the data has been omitted):

    {
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)",
        "value": [
            {
                "@odata.type": "#microsoft.graph.driveItem",
                "id": "<itemId>",
                "name": "Folder name",
                "parentReference": {
                    "driveId": "<myUserId>",
                    "driveType": "personal"
                },
                "remoteItem": {
                    "id": "<remoteItemId>",
                    "name": "Folder name",
                    "createdBy": 
                        "user": {
                            "displayName": "Other user name",
                            "id": "<otherUserId>"
                        }
                    },
                    "folder": {
                        "childCount": 0
                    },
                    "parentReference": {
                        "driveId": "<otherUserId>",
                        "driveType": "personal"
                    },
                    "shared": {
                        "owner": {
                            "user": {
                                "displayName": "Other user name",
                                "id": "<otherUserId>"
                            }
                        }
                    }
                }
            }
        ]
    }
    
    1. Then I preformed PUT request with the following data:

    PUT /drives/{otherUserId}/items/{remoteItemId}:/test.txt:/content

    Content-Type: text/plain
    The contents of the file goes here.
    

    Response: Success - Status Code 201

    {
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('<otherUserId>')/items/$entity",
        "id": "<itemId>",
        "name": "test.txt",
        "size": 35,
        "createdBy": {
            "application": {
                "displayName": "Graph explorer"
            },
            "user": {
                "displayName": "My user name",
                "id": "<myUserId>"
            }
        },
        "parentReference": {
            "driveId": "<otherUserId>",
            "driveType": "personal",
            "id": "<parentReferenceId>",
            "name": "Folder name",
            "path": "/drives/<otherUserId>/items/<parentReferenceId>"
        },
        "file": {
            "mimeType": "text/plain"
        }
    }
    

    Then, on the subsequent GET /me/drive/sharedWithMe request, the value of the childCount of the folder has been increased to 1.

    NOTE:

    The \shares endpoint only allows GET requests to access a shared DriveItem or a collection of shared items. It does not allow to create new items.