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 Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,607 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

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