Search code examples
c#microsoft-graph-apionedrive

How to specify @microsoft.graph.conflictBehavior in the request body in onedriveAPI


I'm working on a C# project with these requirements:

  1. Create a Folder if it doesn't exits
  2. Check if the already exists, if it exists increment the file name.

From Onedrive API documentation Create a new Folder in OneDrive, it says that setting @microsoft.graph.conflictBehavior=rename would increment the folder value if it exists

how can I add the @microsoft.graph.conflictBehavior into my request?

Here's the code which creates the folder using drive Item

var foldertoCreate = new DriveItem {
    Name = $"TestFolder",
    Folder = new Folder (),

};

var newFolder = await _graphClient.Drive
    .Items["MyParent_Item_Id"]
    .Children
    .Request ()
    .AddAsync (foldertoCreate);

Solution

  • I believe you should be able to add the annotation manually via AdditionalData. Obviously this isn't ideal, but I cannot see another way to do it with the current SDK.

    var foldertoCreate = new DriveItem
    {
        Name = $"TestFolder",
        Folder = new Folder(),
        AdditionalData = new Dictionary<string, object>
        {
            { "@microsoft.graph.conflictBehavior", "rename" }
        },
    };
    
    var newFolder = await _graphClient.Drive
        .Items["MyParent_Item_Id"]
        .Children
        .Request()
        .AddAsync(foldertoCreate);