// Builds a FilesViewDisplayModel // folderId: ID of the folder to get files and folders from. If null, gets from the root // pageRequestUrl: Used for paging requests to get the next set of results private async Task <IActionResult> GetViewForFolder(string folderId = null, string pageRequestUrl = null) { var model = new FilesViewDisplayModel(); try { var graphClient = GetGraphClientForScopes(_filesScopes); // Get selected folder IDriveItemRequest folderRequest; if (string.IsNullOrEmpty(folderId)) { // Get the root // GET /me/drive/root folderRequest = graphClient.Me .Drive .Root .Request(); } else { // GET /me/drive/items/folderId folderRequest = graphClient.Me .Drive .Items[folderId] .Request(); } // Send the request model.SelectedFolder = await folderRequest // Only select the fields used by the app .Select(d => new { d.Id, d.Name, d.ParentReference }) .GetAsync(); // Get files and folders IDriveItemChildrenCollectionRequest itemRequest; // Is this a page request? if (!string.IsNullOrEmpty(pageRequestUrl)) { // Instead of using the request builders, // initialize the request directly from the supplied // URL itemRequest = new DriveItemChildrenCollectionRequest( pageRequestUrl, graphClient, null); } else if (string.IsNullOrEmpty(folderId)) { // No folder ID specified, so use /me/drive/root/children // to get all items in the root of user's drive // GET /me/drive/root/children itemRequest = graphClient.Me .Drive .Root .Children .Request(); } else { // Folder ID specified // GET /me/drive/items/folderId/children itemRequest = graphClient.Me .Drive .Items[folderId] .Children .Request(); } if (string.IsNullOrEmpty(pageRequestUrl)) { itemRequest = itemRequest .Top(GraphConstants.PageSize) // Only get the fields used by the view .Select(d => new { d.File, d.FileSystemInfo, d.Folder, d.Id, d.LastModifiedBy, d.Name, d.ParentReference }) .Expand("thumbnails"); } // Get max PageSize number of results var driveItemPage = await itemRequest .GetAsync(); model.Files = new List <DriveItem>(); model.Folders = new List <DriveItem>(); foreach (var item in driveItemPage.CurrentPage) { if (item.Folder != null) { model.Folders.Add(item); } else if (item.File != null) { model.Files.Add(item); } } model.NextPageUrl = driveItemPage.NextPageRequest? .GetHttpRequestMessage().RequestUri.ToString(); return(View("Index", model)); } catch (ServiceException ex) { InvokeAuthIfNeeded(ex); return(RedirectToAction("Error", "Home") .WithError($"Error getting files", ex.Error.Message)); } }
public async Task <IActionResult> SharedWithMe(string pageUrl = null) { var model = new FilesViewDisplayModel(); var scopes = new[] { GraphConstants.FilesReadWriteAll }; try { var graphClient = GetGraphClientForScopes(scopes); IDriveSharedWithMeRequest sharedItemsRequest; // Is this a page request? if (string.IsNullOrEmpty(pageUrl)) { // Not a page request, use the Graph client // to build the request // GET /me/drive/sharedWithMe sharedItemsRequest = graphClient.Me .Drive .SharedWithMe() .Request(); } else { // This is a page request, so instead of using // the request builders, initialize the request directly // from the URL sharedItemsRequest = new DriveSharedWithMeRequest( pageUrl, graphClient, null); } // Send the request var sharedItemPage = await sharedItemsRequest.GetAsync(); // Results can include folders and/or files, so // go through them and add them to the appropriate list model.Files = new List <DriveItem>(); model.Folders = new List <DriveItem>(); foreach (var item in sharedItemPage.CurrentPage) { if (item.RemoteItem.Folder != null) { model.Folders.Add(item); } else if (item.RemoteItem.File != null) { model.Files.Add(item); } } model.NextPageUrl = sharedItemPage.NextPageRequest? .GetHttpRequestMessage().RequestUri.ToString(); return(View(model)); } catch (ServiceException ex) { InvokeAuthIfNeeded(ex); return(RedirectToAction("Error", "Home") .WithError("Error getting shared items", ex.Error.Message)); } }