Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Latest commit

 

History

History
186 lines (151 loc) · 5.27 KB

items.md

File metadata and controls

186 lines (151 loc) · 5.27 KB

Items in the OneDrive SDK for C#

Items in the OneDrive SDK for C# behave just like items through the OneDrive API. All actions on items described in the OneDrive API are available through the SDK. For more information, see the Items Reference.

The examples below assume that you have Authenticated your app with a OneDriveClient object.

Get an Item

1. By ID

var item = await oneDriveClient
                     .Items[itemId]
                     .Request()
                     .GetAsync();

2. By path

var item = await oneDriveClient
                     .Drive
                     .Root
                     .ItemWithPath("path/to/file/txt")
                     .Request()
                     .GetAsync();

Access an item by parent reference path:

var item = await oneDriveClient
                     .ItemWithPath(parentItem.ParentReference.Path + "/" + parentItem.Name + "/relative/path")
                     .Request()
                     .GetAsync();

Delete an Item

await oneDriveClient
          .Drive
          .Items[itemId]
          .Request()
          .DeleteAsync();

Get children for an Item

More info about collections here.

await oneDriveClient
          .Drive
          .Items[itemId]
          .Children
          .Request()
          .GetAsync();

Create a folder

1. By POST to a known folder ID

var folderToCreate = new Item { Folder = new Folder(), Name = "folder name" };
var createdFolder = await oneDriveClient
          .Drive
          .Items[itemId]
          .Children
          .Request()
          .AddAsync(folderToCreate);

2. By PUT to a known folder ID

var folderToCreate = new Item { Folder = new Folder() };
var createdFolder = await oneDriveClient
          .Drive
          .Items[itemId]
          .ItemWithPath("folder name")
          .Request()
          .CreateAsync(folderToCreate);

3. By PUT with a given path

var folderToCreate = new Item { Folder = new Folder() };
var createdFolder = await oneDriveClient
          .Drive
          .Root
          .ItemWithPath("folder/subfolder")
          .Request()
          .CreateAsync(folderToCreate);

Note: If the specified path does not exist, every folder name in the path will also be created.

Uploading contents

using (contentStream)
{
    var uploadedItem = await oneDriveClient
                                 .Drive
                                 .Root
                                 .ItemWithPath("path/to/file.txt")
                                 .Content
                                 .Request()
                                 .PutAsync<Item>(contentStream);
}

Downloading contents

var contentStream = await oneDriveClient
                              .Drive
                              .Items[itemId]
                              .Content
                              .Request()
                              .GetAsync();

Moving and updating an Item

To move an item you must update its parent reference.

var updateItem = new Item { ParentReference = new ItemReference { Id = newParentId } };
var itemWithUpdates = await oneDriveClient
                                .Drive
                                .Items[itemId]
                                .Request()
                                .UpdateAsync(updateItem);

To change an item's name you could:

var updateItem = new Item { Name = "New name!" };
var itemWithUpdates = await oneDriveClient
                                .Drive
                                .Items[itemId]
                                .Request()
                                .UpdateAsync(updateItem);

Copy an Item

Copying an item is an async action described here.

var asyncStatus = await oneDriveClient
                            .Drive
                            .Items[itemId]
                            .Copy(newItemName, new ItemReference { Id = copyLocationId })
                            .Request()
                            .PostAsync();  

The Copy action returns an IItemCopyAsyncMonitor instance that has a method to poll the monitor URL for completion. The poll method returns the created item on completion.

To poll until the copy action completes:

var newItem = await asyncStatus.CompleteOperationAsync(null, CancellationToken.None);

CompleteOperationAsync takes in an IProgress<AsyncOperationStatus> for reporting back progress status and a CancellationToken for action cancellation. The method will poll until completion unless cancelled.