public static async Task <string> SearchForFileAsync(string fileName) { string fileId = null; try { GraphServiceClient graphClient = AuthenticationHelper.GetAuthenticatedClient(); // Check that this item hasn't already been created. // https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_search IDriveItemSearchCollectionPage searchResults = await graphClient.Me.Drive.Root.Search(fileName).Request().GetAsync(); foreach (var r in searchResults) { if (r.Name == fileName) { fileId = r.Id; } } } catch (Microsoft.Graph.ServiceException e) { Debug.WriteLine("The search for this file failed: " + e.Error.Message); } return(fileId); }
private async void SearchFoldersButton_Click(Object sender, RoutedEventArgs e) { ShowFolders(); try { searchedItems = await graphClient.Me.Drive.Root .Search("tax").Request().GetAsync(); MyFolders = new ObservableCollection <Models.Folder>(); foreach (var item in searchedItems) { if (item.Folder != null) { MyFolders.Add(new Models.Folder { Id = item.Id, Name = item.Name, FileCount = (int)item.Folder.ChildCount }); } } DriveItemCountTextBlock.Text = $"You have {MyFolders.Count()} folders"; FoldersListView.ItemsSource = MyFolders; } catch (ServiceException ex) { DriveItemCountTextBlock.Text = $"We could not get folders: {ex.Error.Message}"; } }
/// <summary> /// Saves attachment to OneDrive under the parent 'Outlook Attachments' and under child 'Email subject' /// </summary> /// <returns> Attachment's url location </returns> internal static async Task <DriveItem> searchFileOneDrive(string accessToken, string filename) { var graphClient = new GraphServiceClient( new DelegateAuthenticationProvider( async(requestMessage) => { requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); })); try { // This method only supports files less than 4MB IDriveItemSearchCollectionPage results = await graphClient.Me.Drive.Root.Search($"{filename}") .Request() .GetAsync(); if (results.CurrentPage.Count < 1) { return(null); } // TODO: Check for Count > 1 // CurrentPage[0] is the parent folder everything after is child. return(results.CurrentPage[0]); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.ToString()); return(null); } }
/// <summary> /// Establish connection to OneDrive, get authentication, obtains links of files with query option, downloads files in temporary place. /// </summary> /// <returns>Returnsl List of files.</returns> public static async Task <List <StorageFile> > DownloadAllFilesFromOneDrive() { GraphServiceClient grSC = await SignInAndInitializeGraphServiceClient().ConfigureAwait(false); search = await GetFilesByQuery(grSC).ConfigureAwait(false); List <DriveItem> oneDriveItems = search.CurrentPage.Select(x => x).ToList(); StorageFile storageFile; string newPath = string.Empty; List <StorageFile> downloadedFiles = new List <StorageFile>(); foreach (var item in oneDriveItems) { var itemUrl = item.AdditionalData.Values.FirstOrDefault().ToString(); var itemName = item.Name; newPath = await ImageDownloadHelper.DownloadImage( itemUrl, itemName).ConfigureAwait(false); storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(newPath)); downloadedFiles.Add(storageFile); } return(downloadedFiles); }
private async void NextPageButton_Tapped(object sender, TappedRoutedEventArgs e) { if (searchInProgress) { searchItems = await searchItems.NextPageRequest.GetAsync(); NextPageButton.IsEnabled = (searchItems.NextPageRequest != null); FileList.ItemsSource = searchItems.CurrentPage; } else { driveItems = await driveItems.NextPageRequest.GetAsync(); NextPageButton.IsEnabled = (driveItems.NextPageRequest != null); FileList.ItemsSource = driveItems.CurrentPage; } }
private async void SearchTextBox_KeyDown(object sender, KeyRoutedEventArgs e) { if (e.Key == VirtualKey.Enter) { string searchString = (sender as TextBox).Text; if (searchString.Length == 0) { return; } searchInProgress = true; searchItems = await graphClient.Me.Drive.Root.Search(searchString).Request() .Top(ITEM_COUNT) .OrderBy($"{currentSortKey} ASC") .GetAsync(); FileList.ItemsSource = searchItems.CurrentPage; NextPageButton.IsEnabled = (searchItems.NextPageRequest != null); } }
private async void SearchFilesButton_Click(Object sender, RoutedEventArgs e) { ShowFiles(); try { if (FoldersListView.SelectedItem != null) { selectedFolder = ((Models.Folder)FoldersListView.SelectedItem); searchedItems = await graphClient.Me.Drive.Items[selectedFolder.Id] .Search("Direct Deposit") .Request().GetAsync(); } MyFiles = new ObservableCollection <Models.File>(); foreach (var file in searchedItems) { MyFiles.Add(new Models.File { Id = file.Id, Name = file.Name, Size = Convert.ToInt64(file.Size), Url = file.WebUrl }); } DriveItemCountTextBlock.Text = $"You have {MyFiles.Count()} files"; FilesListView.ItemsSource = MyFiles; } catch (ServiceException ex) { DriveItemCountTextBlock.Text = $"We could not get files: {ex.Error.Message}"; } }
private static string GetFilesCount(IDriveItemSearchCollectionPage search) { return(search.Count.ToString()); }