Search code examples
microsoft-graph-apionedrive

Is it possible to get full path to a OneDrive item?


I'm using the MS Graph SDK in a C# project to search through people's OneDrives for specific terms in files and folders. When I find one, I've not found a way to get the full path for a DriveItem?

Not the URL path, but the file system path where the owning user stores the file locally.

Is this possible?


Solution

  • When you request the metadata for a drive item you receive for example:

    {
        "@microsoft.graph.downloadUrl": "https://m365x214355-my.sharepoint.com/personal/meganb_m365x214355_onmicrosoft_com/_layouts/15/download.aspx?UniqueId=c8b43b37-bc76-4352-af27-5a8e9f1bbfeb&Translate=false&tempauth=eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAvbTM2NXgyMTQzNTUtbXkuc2hhcmVwb2ludC5jb21AZGNkMjE5ZGQtYmM2OC00YjliLWJmMGItNGEzM2E3OTZiZTM1IiwiaXNzIjoiMDAwMDAwMDMtMDAwMC0wZmYxLWNlMDAtMDAwMDAwMDAwMDAwIiwibmJmIjoiMTUzMzg5NzE3MiIsImV4cCI6IjE1MzM5MDA3NzIiLCJlbmRwb2ludHVybCI6IitjMlZ4YU9UY2hCSEhmK3hyNlhrMExtd0pRMEhQR0hYMmlKaTBMRldhTHM9IiwiZW5kcG9pbnR1cmxMZW5ndGgiOiIxNjkiLCJpc2xvb3BiYWNrIjoiVHJ1ZSIsImNpZCI6Ik16VTVOVFkyWVRjdFlqazVOeTAwWlRsa0xUZ3hNV1V0TlRNMU5UZGhaRGc0WkdVeSIsInZlciI6Imhhc2hlZHByb29mdG9rZW4iLCJzaXRlaWQiOiJaRGd5TXpFeVpqa3RZakl6WWkwMFkySmpMVGsxWkRVdE0yVXdaRGswWlRZNFl6RmwiLCJhcHBfZGlzcGxheW5hbWUiOiJhcGlzYW5kYm94cHJveHkiLCJhcHBpZCI6IjAwOTFlNTllLTE3ZTgtNDdmMC1hNDUwLTQ0M2Q0ZGQzYTAwNiIsInRpZCI6ImRjZDIxOWRkLWJjNjgtNGI5Yi1iZjBiLTRhMzNhNzk2YmUzNSIsInVwbiI6Im1lZ2FuYkBtMzY1eDIxNDM1NS5vbm1pY3Jvc29mdC5jb20iLCJwdWlkIjoiMTAwM0JGRkRBMzgxMzFBRiIsInNjcCI6ImFsbHNpdGVzLnJlYWQgbXlmaWxlcy5yZWFkIGdyb3VwLnJlYWQgbXlmaWxlcy5yZWFkIGFsbHNpdGVzLnJlYWQgYWxsc2l0ZXMucmVhZCBteWZpbGVzLnJlYWQgYWxscHJvZmlsZXMucmVhZCBhbGxwcm9maWxlcy5yZWFkIiwidHQiOiIyIiwidXNlUGVyc2lzdGVudENvb2tpZSI6bnVsbH0.eWQzcTFxNmhBb0txNFNnZzQyNlIyU2gzaUQwSGJxbC9abWRlVVZ3OGUwMD0&ApiVersion=2.0",
        "createdDateTime": "2017-08-07T16:17:44Z",
        "eTag": "\"{C8B43B37-BC76-4352-AF27-5A8E9F1BBFEB},2\"",
        "id": "01BYE5RZZXHO2MQ5V4KJB26J22R2PRXP7L",
        "lastModifiedDateTime": "2017-08-07T16:17:44Z",
        "name": "Camera Customer Analysis.xlsx",
        "webUrl": "https://m365x214355-my.sharepoint.com/personal/meganb_m365x214355_onmicrosoft_com/_layouts/15/Doc.aspx?sourcedoc=%7BC8B43B37-BC76-4352-AF27-5A8E9F1BBFEB%7D&file=Camera%20Customer%20Analysis.xlsx&action=default&mobileredirect=true",
        "cTag": "\"c:{C8B43B37-BC76-4352-AF27-5A8E9F1BBFEB},1\"",
        "size": 2675045,
        "createdBy": {
            "user": {
                "email": "MeganB@M365x214355.onmicrosoft.com",
                "id": "48d31887-5fad-4d73-a9f5-3c356e68a038",
                "displayName": "Megan Bowen"
            }
        },
        "lastModifiedBy": {
            "user": {
                "email": "MeganB@M365x214355.onmicrosoft.com",
                "id": "48d31887-5fad-4d73-a9f5-3c356e68a038",
                "displayName": "Megan Bowen"
            }
        },
        "parentReference": {
            "driveId": "b!-RIj2DuyvEyV1T4NlOaMHk8XkS_I8MdFlUCq1BlcjgmhRfAj3-Z8RY2VpuvV_tpd",
            "driveType": "business",
            "id": "01BYE5RZ6TAJHXA5GMWZB2HDLD7SNEXFFU",
            "path": "/drive/root:/CR-227 Project"
        },
        "file": {
            "mimeType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            "hashes": {
                "quickXorHash": "qz9+NLaGwZCFBcd9gH+HCNUVcn8="
            }
        },
        "fileSystemInfo": {
            "createdDateTime": "2017-08-07T16:17:44Z",
            "lastModifiedDateTime": "2017-08-07T16:17:44Z"
        }
    }
    

    As you can see it contains a parentReference which contains the path To get the full path, substring the path starting at the first : and append the name. In the above example it would give: /CR-227 Project/Camera Customer Analysis.xlsx

    Doc reference

    However, getting the local path where the user stored the file locally is not stored by Microsoft. But you can reproduce it because the OneDrive file structure will be the same as the local one.