/// <summary> /// Create children http request with specific options /// </summary> /// <param name="top">The number of items to return in a result set.</param> /// <param name="orderBy">Sort the order of items in the response collection</param> /// <param name="filter">Filters the response based on a set of criteria.</param> /// <returns>Returns the http request</returns> private IDriveItemChildrenCollectionRequest CreateChildrenRequest(int top, OrderBy orderBy = OrderBy.None, string filter = null) { IDriveItemChildrenCollectionRequest oneDriveitemsRequest = null; if (orderBy == OrderBy.None && string.IsNullOrEmpty(filter)) { return(((IDriveItemRequestBuilder)RequestBuilder).Children.Request().Top(top)); } if (orderBy == OrderBy.None) { return(((IDriveItemRequestBuilder)RequestBuilder).Children.Request().Top(top).Filter(filter)); } string order = $"{orderBy} asc".ToLower(); if (string.IsNullOrEmpty(filter)) { oneDriveitemsRequest = ((IDriveItemRequestBuilder)RequestBuilder).Children.Request().Top(top).OrderBy(order); } else { oneDriveitemsRequest = ((IDriveItemRequestBuilder)RequestBuilder).Children.Request().Top(top).OrderBy(order).Filter(filter); } return(oneDriveitemsRequest); }
/// <summary> /// Create children http request with specific options /// </summary> /// <param name="requestBuilder">request builder</param> /// <param name="top">The number of items to return in a result set.</param> /// <param name="orderBy">Sort the order of items in the response collection</param> /// <param name="filter">Filters the response based on a set of criteria.</param> /// <returns>Returns the http request</returns> internal static IDriveItemChildrenCollectionRequest CreateChildrenRequest(this IDriveItemRequestBuilder requestBuilder, int top, OrderBy orderBy = OrderBy.None, string filter = null) { IDriveItemChildrenCollectionRequest oneDriveitemsRequest = null; if (orderBy == OrderBy.None && string.IsNullOrEmpty(filter)) { return(requestBuilder.Children.Request().Top(top)); } if (orderBy == OrderBy.None) { return(requestBuilder.Children.Request().Top(top).Filter(filter)); } string order = OneDriveHelper.TransformOrderByToODataString(orderBy); if (string.IsNullOrEmpty(filter)) { oneDriveitemsRequest = requestBuilder.Children.Request().Top(top).OrderBy(order); } else { oneDriveitemsRequest = requestBuilder.Children.Request().Top(top).OrderBy(order).Filter(filter); } return(oneDriveitemsRequest); }
private async Task <IEnumerable <T> > GetPageGraphSdkAsync(int pageSize, CancellationToken cancellationToken = default(CancellationToken)) { // First Call if (_isFirstCall) { _nextPageGraph = ((IDriveItemRequestBuilder)_requestBuilder).CreateChildrenRequest(pageSize, _orderBy, _filter); _isFirstCall = false; } if (cancellationToken.IsCancellationRequested) { return(null); } if (_nextPageGraph != null) { var oneDriveItems = await _nextPageGraph.GetAsync(cancellationToken); _nextPageGraph = oneDriveItems.NextPageRequest; return(ProcessResultGraphSdk(oneDriveItems)); } // no more data return(null); }
private async Task LoadNextPageAsync() { try { if (_nextPageRequest != null) { Task <IDriveItemChildrenCollectionPage> taskItems = _nextPageRequest.GetAsync(_cancelLoadFile.Token); IDriveItemChildrenCollectionPage items = await taskItems; if (!taskItems.IsCanceled) { foreach (DriveItem item in items) { _list.Items.Add(item); } _nextPageRequest = items.NextPageRequest; HasMore = _nextPageRequest != null; } } } catch (Exception exception) { MessageDialog messageDialog = new MessageDialog(exception.Message); await messageDialog.ShowAsync(); } }
private async Task LoadFilesAsync(string driveItemId, int pageIndex = 0) { IsDetailPaneVisible = false; HideDetailsPane(); if (!string.IsNullOrEmpty(_driveId)) { try { _cancelLoadFile.Cancel(false); _cancelLoadFile.Dispose(); _cancelLoadFile = new CancellationTokenSource(); _list.Items.Clear(); VisualStateManager.GoToState(this, NavStatesFolderReadonly, false); QueryOption queryOption = new QueryOption("$top", PageSize.ToString()); await GraphService.TryLoginAsync(); GraphServiceClient graphServiceClient = GraphService.GraphProvider; Task <IDriveItemChildrenCollectionPage> taskFiles = graphServiceClient.Drives[_driveId].Items[driveItemId].Children.Request(new List <Option> { queryOption }).GetAsync(_cancelLoadFile.Token); IDriveItemChildrenCollectionPage files = await taskFiles; if (!taskFiles.IsCanceled) { _list.Items.Clear(); foreach (DriveItem file in files) { _list.Items.Add(file); } _nextPageRequest = files.NextPageRequest; HasMore = _nextPageRequest != null; VisualStateManager.GoToState(this, NavStatesFolderReadonly, false); _pathVisualState = NavStatesFolderReadonly; if (_driveItemPath.Count > 1) { IDriveItemPermissionsCollectionPage permissions = await graphServiceClient.Drives[_driveId].Items[driveItemId].Permissions.Request().GetAsync(); foreach (Permission permission in permissions) { if (permission.Roles.Contains("write") || permission.Roles.Contains("owner")) { VisualStateManager.GoToState(this, NavStatesFolderEdit, false); _pathVisualState = NavStatesFolderEdit; break; } } } else { _pathVisualState = NavStatesFolderEdit; VisualStateManager.GoToState(this, NavStatesFolderEdit, false); } } } catch (Exception) { } } }
/// <summary> /// Initializes the NextPageRequest property. /// </summary> public void InitializeNextPageRequest(IBaseClient client, string nextPageLinkString) { if (!string.IsNullOrEmpty(nextPageLinkString)) { this.NextPageRequest = new DriveItemChildrenCollectionRequest( nextPageLinkString, client, null); } }
/// <summary> /// Request a list of DriveItem from oneDrive and create a GraphOneDriveStorageItemsCollection Collection /// </summary> /// <param name="request">Http request to execute</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>When this method completes successfully, it returns GraphOneDriveStorageItemsCollection that represents the specified files or folders</returns> private async Task <OneDriveStorageItemsCollection> RequestOneDriveItemsAsync(IDriveItemChildrenCollectionRequest request, CancellationToken cancellationToken) { var oneDriveItems = await request.GetAsync(cancellationToken).ConfigureAwait(false); _nextPageItemsRequest = oneDriveItems.NextPageRequest; List <OneDriveStorageItem> items = new List <OneDriveStorageItem>(); foreach (var oneDriveItem in oneDriveItems) { items.Add(InitializeOneDriveStorageItem(oneDriveItem)); } return(new OneDriveStorageItemsCollection(items)); }
/// <summary> /// Request a list of Folder from oneDrive and create a MicrosoftGraphOneDriveItemCollection Collection /// </summary> /// <param name="request">Http request to execute</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>When this method completes successfully, it returns GraphOneDriveStorageItemsCollection that represents the specified files or folders</returns> private async Task <List <OneDriveStorageFolder> > RequestOneDriveFoldersAsync(IDriveItemChildrenCollectionRequest request, CancellationToken cancellationToken) { var oneDriveItems = await request.GetAsync(cancellationToken); _nextPageFoldersRequest = oneDriveItems.NextPageRequest; List <DriveItem> oneDriveFolders = QueryFolders(oneDriveItems); List <OneDriveStorageFolder> folders = new List <OneDriveStorageFolder>(); foreach (var oneDriveFolder in oneDriveFolders) { folders.Add(InitializeOneDriveStorageFolder(oneDriveFolder)); } return(folders); }
private async Task ProcessFiles(IDriveItemChildrenCollectionRequest childrenCollectionRequest, string path) { var driveItems = await childrenCollectionRequest.GetAsync(); var items = new List <DriveItem>(driveItems.CurrentPage); while (driveItems.NextPageRequest != null) { var request = driveItems .NextPageRequest; var response = await request.GetAsync(); items.AddRange(response.CurrentPage); driveItems = response; } foreach (var item in items) { if (item.Folder != null) { var request = _graphserviceClient .Me .Drive .Items[item.Id] .Children .Request(); Console.Write("O"); await ProcessFiles(request, $"{path}/{item.Name}"); } else { var file = new File { Id = item.Id, Name = item.Name, Uri = item.WebUrl, Path = path, Size = item.Size.GetValueOrDefault(), MimeType = item.File?.MimeType, Sha1Hash = item.File?.Hashes?.Sha1Hash }; _items.Add(file); Console.Write("o"); } } }
/// <summary> /// Request a list of items /// </summary> /// <param name="request">Http request to execute</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>When this method completes successfully, it returns a list of IOneDriveStorageFile that represents the specified files</returns> private async Task <List <OneDriveStorageFile> > RequestOneDriveFilesAsync(IDriveItemChildrenCollectionRequest request, CancellationToken cancellationToken = default(CancellationToken)) { var oneDriveItems = await request.GetAsync(cancellationToken); _nextPageFilesRequest = oneDriveItems.NextPageRequest; // TODO: The first items on the list are never a file List <DriveItem> oneDriveFiles = QueryFiles(oneDriveItems); // TODO: Algo to get only File List <OneDriveStorageFile> files = new List <OneDriveStorageFile>(); foreach (var oneDriveFile in oneDriveFiles) { files.Add(InitializeOneDriveStorageFile(oneDriveFile)); } return(files); }
/// <summary> /// Gets an index-based range of files and folders from the list of all files and subfolders in the current folder. /// </summary> /// <param name="startIndex">The zero-based index of the first item in the range to get</param> /// <param name="maxItemsToRetrieve">The maximum number of items to get</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>When this method completes successfully, it returns a list of the subfolders and files in the current folder.</returns> public async Task <IReadOnlyList <OneDriveStorageItem> > GetItemsAsync(uint startIndex, uint maxItemsToRetrieve, CancellationToken cancellationToken = default(CancellationToken)) { IDriveItemChildrenCollectionRequest oneDriveitemsRequest = null; var request = ((IDriveItemRequestBuilder)RequestBuilder).Children.Request(); // skip is not working right now // oneDriveitemsRequest = request.Top((int)maxItemsToRetrieve).Skip((int)startIndex); int maxToRetrieve = (int)(maxItemsToRetrieve + startIndex); oneDriveitemsRequest = request.Top(maxToRetrieve); var tempo = await oneDriveitemsRequest.GetAsync(cancellationToken).ConfigureAwait(false); List <OneDriveStorageItem> items = new List <OneDriveStorageItem>(); for (int i = (int)startIndex; i < maxToRetrieve && i < tempo.Count; i++) { items.Add(InitializeOneDriveStorageItem(tempo[i])); } return(new OneDriveStorageItemsCollection(items)); }
public static async Task <DriveItem[]> GetAllAsync(this IDriveItemChildrenCollectionRequest pagedCollectionRq) { var list = new List <DriveItem>(); var collectionRequest = pagedCollectionRq; while (true && collectionRequest != null) { var pageList = await collectionRequest.GetAsync(); if (pageList.CurrentPage.Count > 0) { list.AddRange(pageList.CurrentPage); collectionRequest = pageList.NextPageRequest; } else { break; } } return(list.ToArray()); }
private async Task LoadNextPageAsync() { try { if (_nextPageRequest != null) { Task <IDriveItemChildrenCollectionPage> taskItems = _nextPageRequest.GetAsync(_cancelLoadFile.Token); IDriveItemChildrenCollectionPage items = await taskItems; if (!taskItems.IsCanceled) { foreach (DriveItem item in items) { _list.Items.Add(item); } _nextPageRequest = items.NextPageRequest; HasMore = _nextPageRequest != null; } } } catch { } }
public async System.Threading.Tasks.Task TestBatch() { IUserRequest meRequest = graphClient.Me.Request(); IGraphServiceUsersCollectionRequest newUserRequest = graphClient.Users.Request(); // We have the ./users URL, query parameters, and request headers. User newUser = new User(); newUser.DisplayName = "New User"; newUser.UserPrincipalName = "*****@*****.**"; IDirectoryObjectWithReferenceRequest managerRequest = graphClient.Me.Manager.Request(); // We have the /me/manager URL, query parameters, and request headers. IDriveItemChildrenCollectionRequest driveRequest = graphClient.Me.Drive.Root.Children.Request(); // We have the /me/drive/root/children URL, query parameters, and request headers. IEducationRootRequest eduRequest = graphClient.Education.Request(); // We have the /education URL, query parameters, and request headers. BatchContainer batchContainer = new BatchContainer(); BatchPart part1 = batchContainer.AddToBatch(meRequest, Method.GET); // I don't think we need a copy of the BatchPart. batchContainer.AddToBatch(driveRequest, Method.GET); batchContainer.AddToBatch(eduRequest, Method.GET); batchContainer.AddToBatch(newUserRequest, Method.POST, 4, newUser, new BatchPart[] { part1 }); // We have to use reflection to determine which HttpVerb method we are using, and then, what // the return type will be. This might be costly batch scenario can contain a large number BatchResponse response = await graphClient.Batch.PostAsync(batchContainer); // of requests across many batches. I think we want to avoid reflection. User me = (User)response.batchResponses.Where(i => i.id == 1).First().body; // No auto-deserialization. User me2 = (User)response.batchResponses.Where(i => i.body.GetType() == typeof(User)).FirstOrDefault().body; foreach (BatchResponsePart part in response.batchResponses) { var responseItem = part.body; // If we deserialize into a dynamic object, the customer would have int statusCode = part.status; } Assert.IsNotNull(me.UserPrincipalName); }
/// <summary> /// Gets the items from the current folder. /// </summary> /// <param name="top">The number of items to return in a result set.</param> /// <param name="orderBy">Sort the order of items in the response collection</param> /// <param name="filter">Filters the response based on a set of criteria.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>When this method completes successfully, it returns a list of the subfolders and files in the current folder.</returns> public async Task <IReadOnlyList <OneDriveStorageItem> > GetItemsAsync(int top, OrderBy orderBy = OrderBy.None, string filter = null, CancellationToken cancellationToken = default(CancellationToken)) { IDriveItemChildrenCollectionRequest oneDriveItemsRequest = ((IDriveItemRequestBuilder)RequestBuilder).CreateChildrenRequest(top, orderBy, filter); return(await RequestOneDriveItemsAsync(oneDriveItemsRequest, cancellationToken).ConfigureAwait(false)); }
/// <summary> /// Gets the subfolders in the current folder. /// </summary> /// <param name="top">The number of items to return in a result set.</param> /// <param name="orderBy">Sort the order of items in the response collection</param> /// <param name="filter">Filters the response based on a set of criteria.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>When this method completes successfully, it returns a list of the subfolders in the current folder.</returns> public Task <List <OneDriveStorageFolder> > GetFoldersAsync(int top = 100, OrderBy orderBy = OrderBy.None, string filter = null, CancellationToken cancellationToken = default(CancellationToken)) { IDriveItemChildrenCollectionRequest oneDriveItemsRequest = CreateChildrenRequest(top, orderBy, filter); return(RequestOneDriveFoldersAsync(oneDriveItemsRequest, cancellationToken)); }
public static async Task <DriveItem[]> GetAllAsync(this IDriveItemChildrenCollectionRequest request) { var collectionPage = await request.GetAsync(); return(await GetAllAsync(collectionPage)); }