/// <summary> /// Creates (if necessary) and returns a Working folder. /// </summary> public static async Task<ODItem> SetWorkingFolder(string folderName) { EnsureConnection(); // Connect to OneDrive root. var rootFolder = await Connection.GetRootItemAsync(ItemRetrievalOptions.DefaultWithChildren); // Search working folder var existingItem = (from f in rootFolder.Children where f.Name == folderName select f).FirstOrDefault(); if (existingItem != null) { if (existingItem.Folder == null) { // There is already a file with this name. throw new Exception("There is already a file with this name."); } else { // The folder already exists. WorkingFolder = existingItem; return WorkingFolder; } } var newFolder = await Connection.CreateFolderAsync(rootFolder.ItemReference(), folderName); WorkingFolder = newFolder; return WorkingFolder; }
private async void ProcessFolder(ODItem folder) { if (null != folder) { this.CurrentFolder = folder; LoadProperties(folder); ODItemCollection pagedItemCollection = await folder.PagedChildrenCollectionAsync(Connection, ChildrenRetrievalOptions.DefaultWithThumbnails); LoadChildren(pagedItemCollection, false); while (pagedItemCollection.MoreItemsAvailable()) { pagedItemCollection = await pagedItemCollection.GetNextPage(Connection); LoadChildren(pagedItemCollection, false); } } }
private async Task DownloadAndSaveItem(ODItem item) { var dialog = new SaveFileDialog(); dialog.FileName = item.Name; dialog.Filter = "All Files (*.*)|*.*"; var result = dialog.ShowDialog(); if (result != System.Windows.Forms.DialogResult.OK) return; var stream = await item.GetContentStreamAsync(Connection, StreamDownloadOptions.Default); using (var outputStream = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Create)) { await stream.CopyToAsync(outputStream); } }
private void FixBreadCrumbForCurrentFolder(ODItem folder) { var breadcrumbs = flowLayoutPanelBreadcrumb.Controls; bool existingCrumb = false; foreach (LinkLabel crumb in breadcrumbs) { if (crumb.Tag == folder) { RemoveDeeperBreadcrumbs(crumb); existingCrumb = true; break; } } if (!existingCrumb) { LinkLabel label = new LinkLabel(); label.Text = "> " + folder.Name; label.LinkArea = new LinkArea(2, folder.Name.Length); label.LinkClicked += linkLabelBreadcrumb_LinkClicked; label.AutoSize = true; label.Tag = folder; flowLayoutPanelBreadcrumb.Controls.Add(label); } }
private async void NavigateToItemWithChildren(ODItem folder) { FixBreadCrumbForCurrentFolder(folder); await LoadFolderFromId(folder.Id); }
private Control CreateControlForChildObject(ODItem item) { OneDriveTile tile = new OneDriveTile { SourceItem = item, Connection = this.Connection }; tile.Click += ChildObject_Click; tile.DoubleClick += ChildObject_DoubleClick; tile.Name = item.Id; return tile; }
private void RemoveItemFromFolderContents(ODItem itemToDelete) { flowLayoutContents.Controls.RemoveByKey(itemToDelete.Id); }
/// <summary> /// Downloads a file. /// </summary> public static async Task<Stream> DownloadFile(ODItem item) { EnsureConnection(); return await item.GetContentStreamAsync(Connection, StreamDownloadOptions.Default); }
private void AddItemToFolderContents(ODItem obj) { flowLayoutContents.Controls.Add(CreateControlForChildObject(obj)); }
/// <summary> /// Update an item referenced by itemReference with the changes in the changes parameter. /// All fields that are not changed in an Item should be set to null before passing the item /// into this call. /// </summary> /// <param name="itemReference"></param> /// <param name="changes"></param> /// <returns></returns> public async Task<ODItem> PatchItemAsync(ODItemReference itemReference, ODItem changes) { if (!itemReference.IsValid()) throw new ArgumentException("itemReference is not a valid reference."); if (null == changes) throw new ArgumentNullException("changes"); Uri serviceUri = UriForItemReference(itemReference); var request = await CreateHttpRequestAsync(serviceUri, ApiConstants.HttpPatch); await SerializeObjectToRequestBody(changes, request); var response = await GetHttpResponseAsync(request); if (response.StatusCode.IsSuccess()) { return await response.ConvertToDataModel<ODItem>(); } else { throw await response.ToException(); } }
/// <summary> /// Upload a file from a URL. /// </summary> /// <param name="parentItemReference"></param> /// <param name="sourceUrl"></param> /// <param name="destinationFilename"></param> /// <returns></returns> public async Task<ODAsyncTask> UploadFromUrlAsync(ODItemReference parentItemReference, string sourceUrl, string destinationFilename) { if (!destinationFilename.ValidFilename()) throw new ArgumentException("destinationFilename contains invalid characters."); if (!parentItemReference.IsValid()) throw new ArgumentException("parentItemReference was invalid. Requires either an ID or Path"); if (string.IsNullOrEmpty(sourceUrl)) throw new ArgumentNullException("sourceUrl"); Uri serviceUri = UriForItemReference(parentItemReference, ApiConstants.ChildrenRelationshipName); var request = await CreateHttpRequestAsync(serviceUri, ApiConstants.HttpPost); AddAsyncHeaders(request); var uploadItem = new ODItem { Name = destinationFilename, File = new Facets.FileFacet(), SourceUrlAnnotation = sourceUrl }; await SerializeObjectToRequestBody(uploadItem, request); return await AsyncTaskResultForRequest(request, serviceUri); }
/// <summary> /// Deletes a file or a folder. /// </summary> public static async Task<bool> DeleteItem(ODItem item) { EnsureConnection(); return await Connection.DeleteItemAsync(item.ItemReference(), ItemDeleteOptions.Default); }
/// <summary> /// Saves a file in the specified folder. /// </summary> public static async Task<ODItem> SaveFile(ODItem parentItem, string filename, Stream fileContent) { EnsureConnection(); return await Connection.PutNewFileToParentItemAsync(parentItem.ItemReference(), filename, fileContent, ItemUploadOptions.Default); }
private async void renameSelectedItemToolStripMenuItem_Click(object sender, EventArgs e) { var itemToRename = this.SelectedItem; FormInputDialog dialog = new FormInputDialog("Rename selected item", "New item name"); dialog.InputText = itemToRename.Name; var result = dialog.ShowDialog(); if (result != System.Windows.Forms.DialogResult.OK) return; ODItem changes = new ODItem { Name = dialog.InputText }; try { var renamedItem = await Connection.PatchItemAsync(itemToRename.ItemReference(), changes); UpdateItemInFolderContents(itemToRename, renamedItem); } catch (ODException ex) { PresentOneDriveException(ex); } }
private void UpdateItemInFolderContents(ODItem originalItem, ODItem latestItem) { foreach (OneDriveTile tile in flowLayoutContents.Controls) { if (tile.SourceItem == originalItem) { tile.SourceItem = latestItem; tile.Name = latestItem.Id; } } }
private void LoadProperties(ODItem item) { SelectedItem = item; oneDriveObjectBrowser1.SelectedItem = item; }
/// <summary> /// Fetches the files in a folder. /// </summary> public static async Task<IEnumerable<ODItem>> GetFiles(ODItem parentItem) { EnsureConnection(); List<ODItem> result = new List<ODItem>(); var page = await parentItem.PagedChildrenCollectionAsync(Connection, ChildrenRetrievalOptions.Default); var list = from f in page.Collection where f.File != null select f; result.AddRange(list); while (page.MoreItemsAvailable()) { list = from f in page.Collection where f.File != null select f; result.AddRange(list); page = await page.GetNextPage(Connection); } return result; }