public static IEnumerable <User> UsersDeltaQueryEfficient(RequestManager requestManager) { // Step 1: make a request to get the latest delta token, without returning any results. The goal is to obtain the current token, and later download the current resource state - efficiently string latestDeltaTokenUrl = $"{requestManager.GraphClient.BaseUrl}/users/delta?$deltaToken=latest"; var page = new UserDeltaCollectionPage(); page.InitializeNextPageRequest(requestManager.GraphClient, latestDeltaTokenUrl); var emptyPage = page.NextPageRequest.GetAsync().Result; var firstDeltaRequest = GetNextDeltaRequest(emptyPage, requestManager.GraphClient); // Step 2: download the current state of the User collection, efficiently var currentStateDict = UserScenarios.GetAllUsers(requestManager).ToDictionary(u => u.Id); // Step 3: pick up delta changes since before we downloaded the current state var changesSinceCurrentState = ExecuteDeltaCycle(firstDeltaRequest); // Step 4: merge changes into current state foreach (var change in changesSinceCurrentState) { currentStateDict[change.Id] = change; } // Step 5: we now have current state, get the delta link for future changes var deltaRequest = GetNextDeltaRequest(changesSinceCurrentState, requestManager.GraphClient); return(WaitForDeltaChanges(deltaRequest)); }
/// <summary> /// Gets full mailbox content for each user in the tenant. It uses a patters similar to the GetAllGroupsWithMembers scenario - see that one for a detailed description. /// </summary> /// <see cref="GroupScenarios.GetAllGroupsWithMembers(RequestManager)GetAllGroupsWithMembers">The approach taken here is the same as in the scenario for downloading all groups with members.</see> /// <seealso cref="UserScenarios.GetAllUsers(RequestManager)"/> /// <param name="requestManager"></param> /// <returns></returns> public static IEnumerable <User> GetAllUsersWithCompleteMailboxes(RequestManager requestManager) { // Step 1: start downloading user objects using partitioning. We need to get users first, so we can build requests for Messages // we use the same optimized approach for this as in the GetAllUsers scenario IEnumerable <User> users = UserScenarios.GetAllUsers(requestManager); // Step 2: as user objects become available, we queue requests for mailboxes // This collection will be populated with Users for whom we fetched complete mailboxes IEnumerable <User> usersWithMailboxes; // This builder supports creation of requests for collections embedded in the User object, such as Messages using (var builder = UserNestedCollectionsRequestBuilder.GetBuilder(requestManager, out usersWithMailboxes)) { foreach (var user in users) { // initiate the request to get messages. // we use a value for Top smaller than the maximum (1000) as messages tend to be large and requests to Graph may time out. builder.Messages(user).Request().Top(500).GetAsync().Wait(); } } return(usersWithMailboxes); }