Search code examples
microsoft-graph-apionedrivemicrosoft-graph-files

Get all the files in others Onedrive in MS Graph


I am trying to list all files in another user's OneDrive.

I am calling GET /users/{user-id}/drive/items/{item-id}/children but my problem is that I do not know how to get the {item-id}.

I tried using https://graph.microsoft.com/v1.0/users/{userid}/drives but it is not showing any item ids.

Subsequently, once I get the id, I will use the copy command POST /users/{userId}/drive/items/{itemId}/copy to copy items.


Solution

  • If you do not know the ID or Path for the DriveItem, you'll need to start at the root of the Drive and walk the folder structure until your find the file(s) you're looking for.

    Your first call would be to /users/{id|userPrincipalName}/drive/root/children?$select=id,name,folder,file. This will list the children's id, name, and either the folder or file data (depending on the item type) from the root folder:

    {
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('id')/drive/root/children(id,name,folder,file)",
        "value": [
            {
                "@odata.etag": "\"{etag},1\"",
                "id": "folderId",
                "name": "My Folder",
                "folder": {
                    "childCount": 10
                }
            },
            {
                "@odata.etag": "\"{etag},1\"",
                "id": "fileId",
                "name": "filename.ext",
                "file": {
                    "mimeType": "type",
                    "hashes": {
                        "quickXorHash": "hash"
                    }
                }
            }
        ]
    }
    

    You then execute the same call against each of the folders to get their children, repeating the pattern until your find the file you're looking for:

    /users/{id|userPrincipalName}/drive/items/{folderId}/children?$select=id,name,folder,file`
    

    Alternatively, you could simply search for the file using /users/{id|userPrincipalName}/drive/root/search(q='{search-text}').