Microsoft Graph Error - Resource not found for the segment 'root:' when uploading file to OneDrive
Asked Answered
P

1

0

When working on this tutorial on Uploading File to OneDrive from Microsoft Graph OneDrive team, I'm getting the following error at the last line of the code shown below:

Remarks: There are some posts online with a related issue, (such as: This, or this, or this or this or this). But they all seem to have a different context or do not have a response.

Question: What could be the issue, and how can we resolve it

Resource not found for the segment 'root:'

Relevant Code:

GraphServiceClient graphClient = ProviderManager.Instance.GlobalProvider.Graph;

var picker = new Windows.Storage.Pickers.FileOpenPicker();
....
picker.FileTypeFilter.Add("*");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
    // Application now has read/write access to the picked file
    // request 1 - upload small file to user's onedrive

  Stream fileStream = await file.OpenStreamForReadAsync();
  DriveItem uploadedFile = await graphClient.Me.Drive.Root
                                .ItemWithPath(file.Path)
                                .Content
                                .Request()
                                .PutAsync<DriveItem>(fileStream);
}
Palaeogene answered 4/7, 2020 at 4:46 Comment(0)
J
2

.ItemWithPath(file.Path) isn't the path to the file you're uploading, it is the destination path.

For example, if you wanted to upload "SomeFile.txt" to the root of your OneDrive, you would use:

graphClient.Me.Drive // The drive
  .Root // The drive's root folder
  .ItemWithPath("SomeFile.txt") // The destination to write the upload to

The reason this is currently failing is OneDrive doesn't know what to do with a Windows drive path (i.e. C:\Files\Documents\SomeFile.txt). It expects a URL safe drive path (i.e. /Documents/SomeFile.txt).

Jaguar answered 13/7, 2020 at 20:24 Comment(1)
Oh, so for example my OneDrive is mapped to my Windows - 10's file explorer path C:\MyFolder. And then OneDrive folder structure has built-in folders Attachments, Documents, Pictures. Hence, using graphClient.Me.Drive.Root.ItemWithPath("/Documents/SomeFile.txt") would upload the file to OneDrive's built-in folder Documents, correct?Palaeogene

© 2022 - 2024 — McMap. All rights reserved.