private static void OutputUsers(IUserDeltaCollectionPage users) { foreach (var user in users) { Console.WriteLine($"User: {user.Id}, {user.GivenName} {user.Surname}"); } }
static async Task <string> DisplayChangedUsersAndGetDeltaLink(IUserDeltaCollectionPage userPage) { //Iterate through the users foreach (var user in userPage) { if (user.UserPrincipalName != null) { Console.WriteLine(user.UserPrincipalName + "\t\t" + user.DisplayName); } } while (userPage.NextPageRequest != null) { //Console.WriteLine("=== NEXT LINK: " + userPage.NextPageRequest.RequestUrl); //Console.WriteLine("=== SKIP TOKEN: " + userPage.NextPageRequest.QueryOptions[0].Value); userPage = await userPage.NextPageRequest.GetAsync(); foreach (var user in userPage) { if (user.UserPrincipalName != null) { Console.WriteLine(user.UserPrincipalName + "\t\t" + user.DisplayName); } } } //Finally, get the delta link string deltaLink = (string)userPage.AdditionalData["@odata.deltaLink"]; //Console.WriteLine("=== DELTA LINK: " + deltaLink); return(deltaLink); }
static async Task <string> DisplayChangedUsersAndGetDeltaLink(IUserDeltaCollectionPage userPage) { /// <summary> /// Using the first page as a starting point (as the input) /// iterate through the first and subsequent pages, writing out the users in each page /// until you reach the last page (NextPageRequest is null) /// finally set the delta link by looking in the additional data, and return it /// </summary> /// Iterate through the users foreach (var user in userPage) { if (user.UserPrincipalName != null) { Console.WriteLine(user.UserPrincipalName.ToLower() + "\t\t" + user.DisplayName); } } /// <exercise_hint> /// Loop through the pages, writing users until there are no new pages /// This happens when NextPageRequest is null /// <exercise_hint> // ADD CODE HERE // Finally, get the delta link and return it /// <exercise_hint> /// You can find the delta link in AdditionalData /// Check https://developer.microsoft.com/en-us/graph/docs/concepts/delta_query_users for syntax /// <exercise_hint> // ADD CODE HERE AND ALSO REPLACE return null with the deltaLink return(null); }
static async Task <string> DisplayChangedUsersAndGetDeltaLink(IUserDeltaCollectionPage userPage) { /// <summary> /// Using the first page as a starting point (as the input) /// iterate through the first and subsequent pages, writing out the users in each page /// until you reach the last page (NextPageRequest is null) /// finally set the delta link by looking in the additional data, and return it /// </summary> /// Iterate through the users foreach (var user in userPage) { if (user.UserPrincipalName != null) { Console.WriteLine(user.UserPrincipalName.ToLower() + "\t\t" + user.DisplayName); } } if (userPage.NextPageRequest != null) { userPage = await userPage.NextPageRequest.GetAsync(); return(await DisplayChangedUsersAndGetDeltaLink(userPage)); } else { return((string)userPage.AdditionalData["@odata.deltaLink"]); } }
private static IUserDeltaCollectionPage GetUsers(GraphServiceClient graphClient, object deltaLink) { IUserDeltaCollectionPage page; // IF this is the first request (previous=null), then request all users // and include Delta() to request a delta link to be included in the // last page of data if (_previousPage == null) { page = graphClient.Users .Delta() .Request() .Select("Id,GivenName,Surname") .GetAsync() .Result; } // ELSE, not the first page so get the next page of users else { _previousPage.InitializeNextPageRequest(graphClient, deltaLink.ToString()); page = _previousPage.NextPageRequest.GetAsync().Result; } _previousPage = page; return(page); }
} // OutputUsers(IUserDeltaCollectionPage users) private IUserDeltaCollectionPage GetUsers(GraphServiceClient graphClient, object deltaLink) { IUserDeltaCollectionPage page; if (lastPage == null) { page = graphClient .Users .Delta() .Request() .GetAsync() .Result; } else { if (deltaLink == null) { return(null); } else { lastPage.InitializeNextPageRequest(graphClient, deltaLink.ToString()); page = lastPage.NextPageRequest.GetAsync().Result; } } lastPage = page; return(page); } // GetUsers(GraphServiceClient graphClient, object deltaLink)
private static IUserDeltaRequest GetNextDeltaRequest(IUserDeltaCollectionPage currentPage, IGraphServiceClient client) { string deltaLink = GetDeltaLinkFromPage(currentPage); currentPage.InitializeNextPageRequest(client, deltaLink); return(currentPage.NextPageRequest); }
private void OutputUsers(IUserDeltaCollectionPage users) { foreach (var user in users) { var message = $"User: {user.Id}, {user.GivenName} {user.Surname}"; Console.WriteLine(message); } }
} // CheckForUpdates() private void OutputUsers(IUserDeltaCollectionPage users) { foreach (var user in users) { var message = $"User: {user.Id}, {user.GivenName} {user.Surname}"; //_logger.LogWarning(message); } } // OutputUsers(IUserDeltaCollectionPage users)
private static string GetDeltaLinkFromPage(IUserDeltaCollectionPage currentPage) { if (!currentPage.AdditionalData.TryGetValue("@odata.deltaLink", out object value)) { throw new InvalidOperationException("The last page response did not contain the deltaLink."); } return((string)value); }
/// <summary> /// This is a "traditional" way of doing delta query. We download the currest state of the resource (Users collection) and then use delta links to pick up changes. /// The problem with this approach is that it cannot be optimized as we need to page the initial state sequentially. If there are a lot of users, this will take a while. /// </summary> /// <param name="client"></param> /// <returns></returns> public static IEnumerable <User> UserDeltaQueryTraditional(GraphServiceClient client) { //Initialize the delta query IUserDeltaRequest request = client.Users.Delta().Request().Top(999); // Step 1: populate the current state of users by following the nextLink in the collection IUserDeltaCollectionPage currentPage = ExecuteDeltaCycle(request); // Step 2: Get the deltaLink from the last response // Note: the Microsoft.Graph SDK doesn't fully implement delta, so we have to reach into the AdditionalData value bag IUserDeltaRequest deltaRequest = GetNextDeltaRequest(currentPage, client); // Step 3: After changes are made to users, execute the deltaRequest to pick them up return(WaitForDeltaChanges(deltaRequest)); }
private static async Task <string> GetUsersWithDelta(IUserDeltaCollectionPage page, ITargetBlock <User> target, CancellationToken token) { while (page.NextPageRequest != null) { page = await GraphHelper.ExecuteWithRetryAndRateLimit(async() => await page.NextPageRequest.GetAsync(token), token, 0); foreach (User user in page.CurrentPage) { target.Post(user); } } page.AdditionalData.TryGetValue("@odata.deltaLink", out object link); return(link as string); }
private async Task <IUserDeltaCollectionPage> GetUsers(GraphServiceClient graphClient, object deltaLink) { IUserDeltaCollectionPage page; if (lastPage == null) { page = await graphClient.Users .Delta() .Request() .GetAsync(); } else { lastPage.InitializeNextPageRequest(graphClient, deltaLink.ToString()); page = await lastPage.NextPageRequest.GetAsync(); } lastPage = page; return(page); }
private void OutputUsers(IUserDeltaCollectionPage users) { users.ToList().ForEach(user => Console.WriteLine($"User: {user.Id}, {user.GivenName} {user.Surname}")); }
private async static Task <List <UserEntry> > GetUsersAsync(bool force = false) { if (!SettingsService.Instance.Sync.SyncUsers) { throw new ApplicationException("Not configured to sync users."); } if (SettingsService.Instance.Server?.Azure == null) { throw new ApplicationException("No configuration for directory server."); } if (SettingsService.Instance.Sync == null) { throw new ApplicationException("No configuration for sync."); } if (!AuthService.Instance.Authenticated) { throw new ApplicationException("Not authenticated."); } var entries = new List <UserEntry>(); var userRequest = _graphClient.Users.Delta(); IUserDeltaCollectionPage users = null; if (!force && SettingsService.Instance.UserDeltaToken != null) { try { var delataRequest = userRequest.Request().Filter(SettingsService.Instance.Sync.UserFilter); delataRequest.QueryOptions.Add(new QueryOption("$deltatoken", SettingsService.Instance.UserDeltaToken)); users = await delataRequest.GetAsync(); } catch { users = null; } } if (users == null) { users = await userRequest.Request().Filter(SettingsService.Instance.Sync.UserFilter).GetAsync(); } while (true) { foreach (var user in users) { var entry = new UserEntry { ReferenceId = user.Id, ExternalId = user.Id, Email = user.Mail ?? user.UserPrincipalName, Disabled = !user.AccountEnabled.GetValueOrDefault(true) }; object deleted; if (user.AdditionalData.TryGetValue("@removed", out deleted) && deleted.ToString().Contains("changed")) { entry.Deleted = true; } else if (!entry.Disabled && (entry?.Email?.Contains("#") ?? true)) { continue; } entries.Add(entry); } if (users.NextPageRequest == null) { object deltaLink; if (users.AdditionalData.TryGetValue("@odata.deltaLink", out deltaLink)) { var deltaUriQuery = new Uri(deltaLink.ToString()).ParseQueryString(); if (deltaUriQuery["$deltatoken"] != null) { SettingsService.Instance.UserDeltaToken = deltaUriQuery["$deltatoken"]; } } break; } else { users = await users.NextPageRequest.GetAsync(); } } return(entries); }