Error screenshot:
public async Task UploadFileToOneDriveAsync(Stream fileStream, string UserId, string folderName, string fileName)
{
var uploadPath = string.IsNullOrEmpty(folderName) ? $"/{fileName}" :$"/{folderName}/{fileName}";
await _graphClient.Users[UserId].Drive
.Root
.ItemWithPath(uploadPath)
.Content
.Request()
.PutAsync<DriveItem>(fileStream);
}
I have tried using multiple ways
One is
await _graphClient.Users[UserId].Drive
.Root
.ItemWithPath(uploadPath)
.Content
.Request()
.PutAsync<DriveItem>(fileStream);
Second is
await _graphClient.Users[UserId].Drive.Items["root"]
.ItemWithPath(uploadPath)
.Content
.Request()
.PutAsync<DriveItem>(fileStream);
But in both cases it is showing error for root. I am using these package references:
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Microsoft.Graph" Version="5.57.0" />
<PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.7" />
<PackageReference Include="Microsoft.Graph.Core" Version="3.1.21" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.64.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
Please help.
Mahesh Borhade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You are now using Graph SDK to call Ms Graph api. And we could see that the SDK version is 5.57.0
so that we need to obey the V5 version grammer. You can take a look at the migration guidance and you will see the differences, such as removing .Request()
and Drive Item path changes.
If you are trying to upload large file, then you shall follow this API and the codes is similar to
using var fileStream = System.IO.File.OpenRead("filePath");
var uploadSessionBody = new Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession.CreateUploadSessionPostRequestBody
{
// Item = ...
};
// Create the upload session
// itemPath does not need to be a path to an existing item
var myDrive = await _graphServiceClient.Me.Drive.GetAsync();
var uploadSession = await _graphServiceClient.Drives[myDrive?.Id]
.Items["root"]
.ItemWithPath("{itemPath}")
.CreateUploadSession
.PostAsync(uploadSessionBody);
// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320 * 1024;
var fileUploadTask = new LargeFileUploadTask<DriveItem>(
uploadSession, fileStream, maxSliceSize, _graphServiceClient.RequestAdapter);
var totalLength = fileStream.Length;
// Create a callback that is invoked after each slice is uploaded
IProgress<long> progress = new Progress<long>(prog =>
{
Console.WriteLine($"Uploaded {prog} bytes of {totalLength} bytes");
});
try
{
// Upload the file
var uploadResult = await fileUploadTask.UploadAsync(progress);
Console.WriteLine(uploadResult.UploadSucceeded ?
$"Upload complete, item ID: {uploadResult.ItemResponse.Id}" :
"Upload failed");
}
catch (ODataError ex)
{
Console.WriteLine($"Error uploading: {ex.Error?.Message}");
}
If you are trying to upload small files, then the codes shall be similar to codes below.
using var fileStream = System.IO.File.OpenRead("filePath");
var requestInformation = _graphServiceClient.Drives["driveId"].Root
.ItemWithPath("itemPath").Content.ToPutRequestInformation(fileStream);
1