Hi @Ansh , Welcome to Microsoft Q&A,
Updated:
I implemented two API calls to the Microsoft Graph API:
public async Task<HashSet<NameAndEmail>> Search(string searchQuery)
{
try
{
await ConfigureClient();
var uniqueResults = new HashSet<NameAndEmail>(new NameAndEmailComparer());
// First task: Search for people
var task1 = Task.Run(async () =>
{
string url1 = "https://graph.microsoft.com/beta/search/query";
string jsonData = $@"
{{
""requests"": [
{{
""entityTypes"": [""person""],
""query"": {{
""queryString"": ""{searchQuery}""
}},
""From"": 0,
""Size"": 25,
""Fields"": [""DisplayName"", ""EmailAddresses""]
}}
]
}}";
HttpContent content1 = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response1 = await _httpClient.PostAsync(url1, content1);
var responseString1 = await response1.Content.ReadAsStringAsync();
AddUniqueEmails(ExtractEmailsAndDisplayNames(responseString1), uniqueResults);
});
// Second task: Search users by given name
var task2 = Task.Run(async () =>
{
string url2 = $"https://graph.microsoft.com/v1.0/users?$filter=startswith(givenName,'{searchQuery}')";
var response2 = await _httpClient.GetAsync(url2);
var responseString2 = await response2.Content.ReadAsStringAsync();
AddUniqueEmails(ExtractEmail(responseString2), uniqueResults);
});
await Task.WhenAll(task1, task2);
return uniqueResults;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
In this approach, I run two asynchronous tasks: one to search for people using the endpoint and another to filter users with . I then merge the results, ensuring there are no duplicates, using a ./beta/search/querystartswith(givenName, '{searchQuery}')HashSet
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.