/// <summary>
        /// Performs a search of the default Documents folder. Displays the first page of results.
        /// </summary>
        /// <returns>A collection of information that describes files and folders.</returns>
        internal async Task<List<FileSystemItemViewModel>> GetMyFilesAsync()
        {
            var fileResults = new List<FileSystemItemViewModel>();

            try
            {
                var graphClient = await AuthenticationHelper.GetGraphServiceClientAsync();
                var driveItems = await graphClient.Me.Drive.Root.Children.Request().GetAsync();
                foreach (var item in driveItems)
                {
                    FileSystemItemViewModel fileItemModel = new FileSystemItemViewModel();
                    fileItemModel.Name = item.Name;
                    fileItemModel.LastModifiedBy = item.LastModifiedBy.User.DisplayName;
                    fileItemModel.LastModifiedDateTime = item.LastModifiedDateTime.GetValueOrDefault(new DateTime());
                    fileItemModel.Id = item.Id;
                    fileItemModel.Folder = item.Folder != null ? item.Folder.ToString() : string.Empty;
                    fileResults.Add(fileItemModel);
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }

            return fileResults.OrderBy(e => e.Name).ToList();
        }
        /// <summary>
        /// Performs a search of the default Documents folder. Displays the first page of results.
        /// </summary>
        /// <returns>A collection of information that describes files and folders.</returns>
        internal async Task<List<FileSystemItemViewModel>> GetMyFilesAsync()
        {
            var fileResults = new List<FileSystemItemViewModel>();

            try
            {
                var restURL = string.Format("{0}me/drive/root/children", AuthenticationHelper.ResourceBetaUrl);
                string responseString = await AuthenticationHelper.GetJsonAsync(restURL);
                if (responseString != null)
                {
                    var jsonresult = JObject.Parse(responseString);

                    foreach (var item in jsonresult["value"])
                    {
                        FileSystemItemViewModel fileItemModel = new FileSystemItemViewModel();
                        fileItemModel.Name = !string.IsNullOrEmpty(item["name"].ToString()) ? item["name"].ToString() : string.Empty;
                        fileItemModel.LastModifiedBy = !string.IsNullOrEmpty(item["lastModifiedBy"]["user"]["displayName"].ToString()) ? item["lastModifiedBy"]["user"]["displayName"].ToString() : string.Empty;
                        fileItemModel.LastModifiedDateTime = !string.IsNullOrEmpty(item["lastModifiedDateTime"].ToString()) ? DateTime.Parse(item["lastModifiedDateTime"].ToString()) : new DateTime();
                        fileItemModel.Id = !string.IsNullOrEmpty(item["id"].ToString()) ? item["id"].ToString() : string.Empty;
                        fileItemModel.Folder = item["folder"] != null ? item["folder"].ToString() : string.Empty;
                        fileResults.Add(fileItemModel);
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }

            return fileResults.OrderBy(e => e.Name).ToList();
        }
Ejemplo n.º 3
0
    /// <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(FileSystemItemViewModel _selectedFileObject) {
      bool? isSuccess = false;

      try {
        // Gets the FileSystemItem that is selected in the bound ListBox control.
        IItem fileOrFolderToDelete = _selectedFileObject.FileSystemItem;

        // This results in a call to the service.
        await fileOrFolderToDelete.DeleteAsync();

        isSuccess = true;
      } catch (Microsoft.Data.OData.ODataErrorException) {
        isSuccess = null;
      } catch (NullReferenceException) {
        isSuccess = null;
      }

      return isSuccess;
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Downloads a file selected in the ListBox control.
    /// </summary>
    /// <param name="_selectedFileObject">The file selected in the ListBox.</param>
    /// <returns>A Stream of the downloaded file.</returns>
    internal async Task<Stream> DownloadFileAsync(FileSystemItemViewModel _selectedFileObject) {

      File file;
      Stream stream = null;

      try {
        // Get a handle on the selected item.
        IItem myFile = _selectedFileObject.FileSystemItem;
        file = myFile as File;
        // Download the file from the service. This results in call to the service.
        stream = await file.DownloadAsync();
      } catch (NullReferenceException) {
        // Silently fail. A null stream will be handled higher up the stack.
      }

      return stream;
    }
Ejemplo n.º 5
0
    /// <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(FileSystemItemViewModel _selectedFileObject, string fileText) {
      File file;
      byte[] byteArray;
      bool isSuccess = false;

      try {
        // Get a handle on the selected item.
        IItem myFile = _selectedFileObject.FileSystemItem;
        file = myFile as 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;
    }
Ejemplo n.º 6
0
    /// <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(FileSystemItemViewModel _selectedFileObject) {

      string fileContents = string.Empty;
      object[] results = new object[] { fileContents, false };

      try {
        // Get a handle on the selected item.
        IItem myFile = _selectedFileObject.FileSystemItem;

        // Check that the selected item is a text-based file.
        if (!myFile.Name.EndsWith(".txt") && !myFile.Name.EndsWith(".xml")) {
          results[0] = string.Empty;
          results[1] = false;
          return results;
        }

        File file = myFile as File;

        // Download the file contents as 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;
    }