I've seen many questions about how to achieve the User Profile Picture URL via the CSOM (Client-Side-Object-Model) API. However none of the solutions worked or they required the Account Name of the User.
This is what I built up so far:
public static string getUserProfilePicUrl()
{
const string targetUserMail = "[email protected]";
// Getting the target User
ClientContext clientContext = getClientContext();
var users = clientContext.loadQuery(clientContext.Web.SiteUsers.Where(u => u.Mail == targetUserMail));
clientContext.ExecuteQuery();
Microsoft.SharePoint.Client.User targetUser = users.FirstOrDefault();
if (targetUser == null)
return null;
// Getting his "PictureUrl" Property
PeopleManager peopleManager = new PeopleManager(clientContext);
PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser.LoginName); // The LoginName Property has the correct format for the People Manger
clientContext.Load(personProperties, p => p.PictureUrl);
clientContext.ExecuteQuery();
return personProperties.PictureUrl;
}
Sadly this property always is null (tested with various users)
Another way I tried is using the userphoto.aspx page in the ScriptBase:
hostweb/_layouts/15/userphoto.aspx?size=S&accountname=EMAIL_HERE
This call works, but I always get the default picture of a SharePoint Server 2013 and never the picture I uploaded for my profile.
I'm looking for a way to receive the User Profile Picture URL of a User by E-Mail through C# CSOM API. The operation should work for Office 365 AND on-premises solutions.