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
- Delete an Item
- Get Children for an Item
- Create a folder
- Uploading contents
- Downloading contents
- Moving and updating an Item
- Copy an Item
var item = await oneDriveClient
.Items[itemId]
.Request()
.GetAsync();
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();
await oneDriveClient
.Drive
.Items[itemId]
.Request()
.DeleteAsync();
More info about collections here.
await oneDriveClient
.Drive
.Items[itemId]
.Children
.Request()
.GetAsync();
var folderToCreate = new Item { Folder = new Folder(), Name = "folder name" };
var createdFolder = await oneDriveClient
.Drive
.Items[itemId]
.Children
.Request()
.AddAsync(folderToCreate);
var folderToCreate = new Item { Folder = new Folder() };
var createdFolder = await oneDriveClient
.Drive
.Items[itemId]
.ItemWithPath("folder name")
.Request()
.CreateAsync(folderToCreate);
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.
using (contentStream)
{
var uploadedItem = await oneDriveClient
.Drive
.Root
.ItemWithPath("path/to/file.txt")
.Content
.Request()
.PutAsync<Item>(contentStream);
}
var contentStream = await oneDriveClient
.Drive
.Items[itemId]
.Content
.Request()
.GetAsync();
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);
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.