public void InitializeDefaultData() { this.Me = new User(); Me.Mail = "*****@*****.**"; Me.DisplayName = "Test Test"; this.Users = new GraphServiceUsersCollectionPage(); this.People = new UserPeopleCollectionPage(); }
private void TransformGraphDataToDto(IUserPeopleCollectionPage data) { var items = data.ToList(); foreach (var item in items) { var colleague = new Colleague { Name = item?.GivenName, Department = item?.Department, EmailAddress = item?.ScoredEmailAddresses.FirstOrDefault().Address, Title = item.JobTitle }; Colleagues.Add(colleague); } }
/// <inheritdoc/> internal override async Task <IEnumerable <Person> > CallGraphServiceWithResultAsync(IGraphServiceClient client, IReadOnlyDictionary <string, object> parameters, CancellationToken cancellationToken) { string userId = (string)parameters["UserId"]; int maxCount = (int)parameters["MaxResults"]; // Ensure we filter out groups, meeting rooms, etc. which are not actually people IUserPeopleCollectionRequest request = client.Users[userId].People.Request().Filter("personType eq 'Person' and mailboxType eq 'Mailbox'"); if (maxCount > 0) { request = request.Top(maxCount); } IUserPeopleCollectionPage result = await request.GetAsync(cancellationToken).ConfigureAwait(false); return(result.CurrentPage); }
/// <summary> /// Get people whose name contains specified word. /// </summary> /// <param name="name">person name.</param> /// <returns>the persons list.</returns> private async Task <List <Person> > GetMSPeopleAsync(string name) { var items = new List <Person>(); var optionList = new List <QueryOption>(); var filterString = $"\"{name}\""; optionList.Add(new QueryOption("$search", filterString)); // Get the current user's profile. IUserPeopleCollectionPage users = null; try { users = await _graphClient.Me.People.Request(optionList).GetAsync(); } catch (ServiceException ex) { throw GraphClient.HandleGraphAPIException(ex); } // var users = await _graphClient.Users.Request(optionList).GetAsync(); if (users?.Count > 0) { foreach (var user in users) { // Filter out conference rooms. var displayName = user.DisplayName ?? string.Empty; if (!displayName.StartsWith("Conf Room")) { // Get user properties. items.Add(user); } } } return(items); }
/// <inheritdoc/> internal override async Task <List <CalendarSkillContactModel> > CallGraphServiceWithResultAsync(IGraphServiceClient client, IReadOnlyDictionary <string, object> parameters, CancellationToken cancellationToken) { var name = (string)parameters["Name"]; var results = new List <CalendarSkillContactModel>(); var optionList = new List <QueryOption>(); optionList.Add(new QueryOption("$search", $"\"{name}\"")); // Get the current user's profile. IUserContactsCollectionPage contacts = await client.Me.Contacts.Request(optionList).Select("displayName,emailAddresses,imAddresses").GetAsync(cancellationToken).ConfigureAwait(false); var contactsResult = new List <CalendarSkillContactModel>(); if (contacts?.Count > 0) { foreach (var contact in contacts) { var emailAddresses = contact.EmailAddresses.Where(e => this.IsEmail(e.Address)).Select(e => e.Address).ToList(); if (!emailAddresses.Any()) { emailAddresses = contact.ImAddresses.Where(e => this.IsEmail(e)).ToList(); } if (emailAddresses.Any()) { // Get user properties. contactsResult.Add(new CalendarSkillContactModel { Name = contact.DisplayName, EmailAddresses = emailAddresses, Id = contact.Id, }); } } } IUserPeopleCollectionPage people = await client.Me.People.Request(optionList).Select("displayName,emailAddresses").GetAsync(cancellationToken).ConfigureAwait(false); if (people?.Count > 0) { var existingResult = new HashSet <string>(contactsResult.SelectMany(c => c.EmailAddresses), StringComparer.OrdinalIgnoreCase); foreach (var person in people) { var emailAddresses = new List <string>(); foreach (var email in person.EmailAddresses) { // If the email address isn't already included in the contacts list, add it if (!existingResult.Contains(email.Address) && this.IsEmail(email.Address)) { emailAddresses.Add(email.Address); } } // Get user properties. if (emailAddresses.Any()) { results.Add(new CalendarSkillContactModel { Name = person.DisplayName, EmailAddresses = emailAddresses, Id = person.Id, }); } } } results.AddRange(contactsResult); return(results); }
private async Task UpdateResultsAsync(string text) { var list = SuggestedItemsSource as IList; if (list == null) { return; } var graph = ProviderManager.Instance.GlobalProvider.GetClient(); if (graph == null) { return; } // If empty, will clear out list.Clear(); if (string.IsNullOrWhiteSpace(text)) { return; } IGraphServiceUsersCollectionPage usersCollection = null; try { // Returns an empty collection if no results found. usersCollection = await graph.FindUserAsync(text); } catch (Microsoft.Graph.ServiceException e) { if (e.StatusCode == System.Net.HttpStatusCode.Forbidden) { // Insufficient privileges. // Incremental consent must not be supported by the current provider. // TODO: Log or handle the lack of sufficient privileges. } else { // Something unexpected happened. throw; } } if (usersCollection != null && usersCollection.Count > 0) { foreach (var user in usersCollection.CurrentPage) { // Exclude people in suggested list that we already have picked if (!Items.Any(person => (person as Person)?.Id == user.Id)) { list.Add(user.ToPerson()); } } } IUserPeopleCollectionPage peopleCollection = null; try { // Returns an empty collection if no results found. peopleCollection = await graph.FindPersonAsync(text); } catch (Microsoft.Graph.ServiceException e) { if (e.StatusCode == System.Net.HttpStatusCode.Forbidden) { // Insufficient privileges. // Incremental consent must not be supported by the current provider. // TODO: Log or handle the lack of sufficient privileges. } else { // Something unexpected happened. throw; } } if (peopleCollection != null && peopleCollection.Count > 0) { // Grab ids of current suggestions var ids = list.Cast <object>().Select(person => (person as Person).Id); foreach (var contact in peopleCollection.CurrentPage) { // Exclude people in suggested list that we already have picked // Or already suggested if (!Items.Any(person => (person as Person)?.Id == contact.Id) && !ids.Any(id => id == contact.Id)) { list.Add(contact); } } } }
private async void SearchBox_OnTextChanged(object sender, TextChangedEventArgs e) { var textboxSender = (TextBox)sender; string searchText = textboxSender.Text.Trim(); if (string.IsNullOrWhiteSpace(searchText)) { ClearAndHideSearchResultListBox(); return; } IsLoading = true; try { var graphService = MicrosoftGraphService.Instance; await graphService.TryLoginAsync(); GraphServiceClient graphClient = graphService.GraphProvider; if (graphClient != null) { var options = new List <QueryOption> { new QueryOption("$search", $"\"{searchText}\""), new QueryOption("$filter", "personType/class eq 'Person' and personType/subclass eq 'OrganizationUser'") }; IUserPeopleCollectionPage peopleList = await graphClient.Me.People.Request(options).GetAsync(); if (peopleList.Any()) { List <Person> searchResult = peopleList.ToList(); // Remove all selected items foreach (Person selectedItem in Selections) { searchResult.RemoveAll(u => u.UserPrincipalName == selectedItem.UserPrincipalName); } SearchResultList.Clear(); var result = SearchResultLimit > 0 ? searchResult.Take(SearchResultLimit).ToList() : searchResult; foreach (var item in result) { SearchResultList.Add(item); } _searchResultListBox.Visibility = Visibility.Visible; } else { ClearAndHideSearchResultListBox(); } } } catch (Exception) { } finally { IsLoading = false; } }
public async Task GetRelatedPeopleASync(RelatedPeopleRequest request, OnGetRelatedPersonCompleted onGetRelatedPersonCompleted, OnGetRelatedPeopleCompleted onGetRelatedPeopleCompleted) { List <Person> persons = new List <Person>(); var graphClient = AuthenticationHelper.Instance.GetAuthenticatedClient(); string filter = "personType/class eq 'Person' and personType/subclass eq 'OrganizationUser'"; if (graphClient != null) { if (request.person.Id == "") { IUserPeopleCollectionPage people = await graphClient.Me.People.Request().Filter(filter).GetAsync(); persons.AddRange(people); } else { try { IUserPeopleCollectionPage people = await graphClient.Users[request.person.Id].People.Request().Filter(filter).GetAsync(); persons.AddRange(people); } catch (Exception) { } } } foreach (Person person in persons) { switch (person.PersonType.Class) { case "Person": PhotoDetail detail = await GetPhotoAsync(person.Id); PersonEntity data = new PersonEntity() { FullName = person.DisplayName, Surname = person.Surname, GivenName = person.GivenName, JobTitle = person.JobTitle, Department = person.Department, OfficeLocation = person.OfficeLocation, PhoneNumber = person.Phones.Any() ? person.Phones.First().Number : "", EmailAddress = person.ScoredEmailAddresses.Any() ? person.ScoredEmailAddresses.First().Address : "", Id = person.Id, PhotoDetail = detail }; request.relatedPerson = data; onGetRelatedPersonCompleted(request); request.relatedPeople.Add(data); break; case "Group": break; } } request.relatedPerson = null; onGetRelatedPeopleCompleted(request); }