Share via

Creating a file with PutAsync (ToPutRequestInformation) is creating missing folders, how do I stop this?

Mark Leavesley 11 Reputation points
2025-04-24T11:00:08.34+00:00

If I try and upload a file to a folder that doesn't exist, the below code will create the missing folder. This is undesirable to say in the least nor can I see that it is documented behaviour.

How do I stop this happening?

var requestInformation = _graphClient
    .Drives[sourceList.Drive.Id]
    .Root
    .ItemWithPath(siteRelativePath + "/" + fileName)
    .Content
    .ToPutRequestInformation(stream);


requestInformation.URI = new Uri(requestInformation.URI.OriginalString + "?@microsoft.graph.conflictBehavior=" + conflictBehaviorValue);
var di = await _graphClient.RequestAdapter.SendAsync(requestInformation, DriveItem.CreateFromDiscriminatorValue);

I can do a check for the folder first, but it slows down the process quite a lot:

var di = await _graphClient.Drives[sourceList.Drive!.Id].Root.ItemWithPath(sanitizedSiteRelativePath.ToString()).GetAsync();

Microsoft Security | Microsoft Graph

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.