Search code examples
c#onedrivespecial-folders

Onedrive special App Folder - creating a new folder


I'm working with the OneDrive SDK and have successfully managed to get a reference to my special App folder.

  _appFolder = await OneDriveClient.Drive.Special.AppRoot.Request().GetAsync();

From here I want to create a sub folder called say "Deep Purple".

Looking at the C# example code I can do this using:

var folderToCreate = new Item { Name = artistName, Folder = new Folder() };
        var newFolder = await OneDriveClient
                                  .Drive
                                  .Items[itemId]
                                  .Children
                                  .Request()
                                  .AddAsync(folderToCreate);

But I'm thinking I already have a reference down to Items[itemId] (my _appFolder is of type Item), so I can just use:

var myNewFolder = await _appFolder.Children.Request().AddAsync(folderToCreate);

But no, as you can see by this image I don't have a Request option.

No Request() Option

I'm clearly misunderstanding something.


Solution

  • Your issue is that you are using a Model (object representations of response data) and trying to get a Request, which is only returned from the type RequestBuilder. You are close, though! If you want to add a file to the children of your app folder (which is of type Folder), then your request would look like this:

    var newFolder = await oneDriveClient
        .Drive
        .Items[_appFolder.Id]
        .Children
        .Request()
        .AddAsync(folderToCreate);
    

    At its core, the SDK relies on the OneDriveClient object to generate requests because it knows how to do things like authentication and URL generation. The Models are just containers for information returned from the service. You can use the information in those containers in concert with the client to generate any request you need.