/// <summary> /// Update the currently selected item by appending new text. /// </summary> /// <param name="_selectedFileObject">The file selected in the ListBox.</param> /// <param name="fileText">The updated text contents of the file.</param> /// <returns>A Boolean value that indicates whether the text file was successfully updated.</returns> internal async Task <bool> UpdateTextFileAsync(string selectedItemID, string fileText) { Microsoft.Office365.SharePoint.FileServices.File file; byte[] byteArray; bool isSuccess = false; try { // Make sure we have a reference to the SharePoint client var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles"); // Get a handle on the selected item. IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var myFile = await thisFileFetcher.ExecuteAsync(); file = myFile as Microsoft.Office365.SharePoint.FileServices.File; string updateTime = "\n\r\n\rLast update at " + DateTime.Now.ToLocalTime().ToString(); byteArray = Encoding.UTF8.GetBytes(fileText + updateTime); using (MemoryStream stream = new MemoryStream(byteArray)) { // Update the file. This results in a call to the service. await file.UploadAsync(stream); isSuccess = true; // We've updated the file. } } catch (ArgumentException) { isSuccess = false; } return(isSuccess); }
/// <summary> /// Deletes the selected item or folder from the ListBox. /// </summary> /// <returns>A Boolean value that indicates whether the file or folder was successfully deleted.</returns> internal async Task <bool?> DeleteFileOrFolderAsync(string selectedItemID) { bool?isSuccess = false; try { // Make sure we have a reference to the SharePoint client var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles"); // Get the file to be removed from the SharePoint service. This results in a call to the service. IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var thisFile = await thisFileFetcher.ExecuteAsync(); // Delete the file or folder. This results in a call to the service. await thisFile.DeleteAsync(); isSuccess = true; } catch (Microsoft.Data.OData.ODataErrorException) { isSuccess = null; } catch (NullReferenceException) { isSuccess = null; } return(isSuccess); }
/// <summary> /// Reads the contents of a text file and displays the results in a TextBox. /// </summary> /// <param name="_selectedFileObject">The file selected in the ListBox.</param> /// <returns>A Boolean value that indicates whether the text file was successfully read.</returns> internal async Task <object[]> ReadTextFileAsync(string selectedItemID) { string fileContents = string.Empty; object[] results = new object[] { fileContents, false }; try { // Make sure we have a reference to the SharePoint client var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles"); // Get a handle on the selected item. IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var myFile = await thisFileFetcher.ExecuteAsync(); // Check that the selected item is a .txt file. if (!myFile.Name.EndsWith(".txt") && !myFile.Name.EndsWith(".xml")) { results[0] = string.Empty; results[1] = false; return(results); } Microsoft.Office365.SharePoint.FileServices.File file = myFile as Microsoft.Office365.SharePoint.FileServices.File; // Download the text file and put the results into a string. This results in a call to the service. using (Stream stream = await file.DownloadAsync()) { using (StreamReader reader = new StreamReader(stream)) { results[0] = await reader.ReadToEndAsync(); results[1] = true; } } } catch (NullReferenceException) { results[1] = false; } catch (ArgumentException) { results[1] = false; } return(results); }
public async Task <ActionResult> Download(string fileId, string lmsParams, string nexturl) { if (!Request.IsAuthenticated) { return(RedirectToAction("SignIn", "Account")); } _factory = new ServiceClientFactory(); var sharepointClient = await _factory.CreateSharepointServicesClientWithAsync("MyFiles"); IItemFetcher thisItemFetcher = sharepointClient.Files.GetById(fileId); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var myFile = await thisFileFetcher.ExecuteAsync(); var file = myFile as Microsoft.Office365.SharePoint.FileServices.File; var filename = HttpUtility.UrlEncode(file.Name.Replace(' ', '_')); var generatedFileName = $"{Server.MapPath("/_Tools/O365_OneDrive_Download")}\\{filename}"; if (Debugger.IsAttached) { generatedFileName = $"{Server.MapPath("~/")}Content\\downloads\\{filename}"; } using (Stream stream = await file.DownloadAsync()) { using (Stream saveFile = System.IO.File.Create(generatedFileName)) { CopyStream(stream, saveFile); } } var redirectUrl = $"/{Server.UrlDecode(lmsParams)}{filename}&nexturl={Server.UrlEncode(nexturl)}"; return(Redirect(redirectUrl)); }
internal async Task <bool> DownloadFileAsync(string selectedItemID) { var results = false; try { // Make sure we have a reference to the SharePoint client var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles"); // Get a handle on the selected item. IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var myFile = await thisFileFetcher.ExecuteAsync(); Microsoft.Office365.SharePoint.FileServices.File file = myFile as Microsoft.Office365.SharePoint.FileServices.File; // Download the file and put the results into a string. This results in a call to the service. using (Stream stream = await file.DownloadAsync()) { // TODO: save file to users locker // Create a new file. If the file already exists, it will be overwritten. using (var fileStream = new FileStream(@"C:\hosting\WebApp1\Downloads\" + myFile.Name, FileMode.Create, FileAccess.Write)) { stream.CopyTo(fileStream); } } results = true; } catch (Exception exc) { throw new Exception("Error occured while downloading file: ", exc); } return(results); }