I currently use the following code to pull information from Azure AD for a company directory:
List<QueryOption> options = new List<QueryOption>();
options.Add(new QueryOption("$filter", "accountEnabled%20eq%20true"));
options.Add(new QueryOption("$select", "displayName,companyName,profilePhoto,userPrincipalName"));
IGraphServiceUsersCollectionPage users = await gsc.Users.Request(options).GetAsync();
userResult.AddRange(users);
while (users.NextPageRequest != null)
{
users = await users.NextPageRequest.GetAsync();
userResult.AddRange(users);
}
It works relatively well, retrieving ~400 users' worth of data in roughly 5 seconds (I believe I can drop that time, but I'm not 100% clear on the best practices for dealing with async calls yet). The issue comes when I implement the following code to pull the user profile photos:
foreach(User u in userResult)
{
Stream photo = await gsc.Users[u.UserPrincipalName].Photo.Content.Request().GetAsync();
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = photo.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
imgMe.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(ms.ToArray());
}
}
This bit raises the page load time to over 30 seconds, and I'm having issues reducing that. So my questions in this specific situation are as follows:
- Why does the original query (where I do specify profilePhoto in the select options) not actually pull the profile photo information?
- What am I doing wrong here that creates such a drastic load time?