コード例 #1
0
        public async Task <ActionResult> Files(string groupId, int?pageSize, string nextLink)
        {
            if (!string.IsNullOrEmpty((string)TempData["error"]))
            {
                ViewBag.ErrorMessage = (string)TempData["error"];
            }

            pageSize = pageSize ?? 25;

            var client = GetGraphServiceClient();

            var request = client.Groups[groupId].Drive.Root.Children.Request().Top(pageSize.Value);

            if (!string.IsNullOrEmpty(nextLink))
            {
                request = new DriveItemChildrenCollectionRequest(nextLink, client, null);
            }

            try
            {
                var results = await request.GetAsync();

                ViewBag.NextLink = null == results.NextPageRequest ? null :
                                   results.NextPageRequest.GetHttpRequestMessage().RequestUri;

                return(View(results));
            }
            catch (ServiceException ex)
            {
                if (ex.Error.Code == "InvalidAuthenticationToken")
                {
                    return(new EmptyResult());
                }
                return(RedirectToAction("Index", "Error", new { message = ex.Error.Message }));
            }
        }
コード例 #2
0
        // 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));
            }
        }