Skip to content

Latest commit

 

History

History
144 lines (117 loc) · 4.25 KB

items.md

File metadata and controls

144 lines (117 loc) · 4.25 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();

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 and 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.