5

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 = "mymail@tenant.onmicrosoft.com";

// 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.

  • User's Picture already uploaded to the SharePoint / sync via User Profile service? just making sure if picture already in photo's folder – Waqas Sarwar MVP May 18 '15 at 15:12
  • It is uploaded in my on-premises environment and I just uploaded it to my Office 365 developer site by using: Office 365 -> My Settings -> Me -> Change Picture. Both SharePoint servers recognize and display it. – Vinz May 18 '15 at 15:16
1

You can use ResolvePrincipal method for retrieving user based on email address

ClientResult<Microsoft.SharePoint.Client.Utilities.PrincipalInfo> persons = Microsoft.SharePoint.Client.Utilities.Utility.ResolvePrincipal(clientContext, clientContext.Web, "mymail@tenant.onmicrosoft.com", Microsoft.SharePoint.Client.Utilities.PrincipalType.User, Microsoft.SharePoint.Client.Utilities.PrincipalSource.All, null, true);
clientContext.ExecuteQuery();
Microsoft.SharePoint.Client.Utilities.PrincipalInfo person = persons.Value;

Once you have this information, you can use the LoginName to query UserProfile and get PictureUrl

  • I sadly still get "null" for the Property "PictureUrl". I gave the App the Permission "User Profiles (Social) -> Read". What am I doing wrong? – Vinz May 18 '15 at 15:07
0

Please try this url, https://outlook.office365.com/owa/service.svc/s/GetPersonaPhoto?email=mymail@tenant.onmicrosoft.com&UA=0&size=HR64x64&sc=1468233338850

This will return user's image, if the profile image available for the user.

The above URL only works in SharePoint Online.

  • 2
    My question title mentions that this question is about the CSOM (Client-Sided-Object-Model) which should be able to retrieve a URL to the User Profile Picture as described by the documentation and various examples. It will also ensure that the code runs on both, on-premises and SharePoint online environments. While your answer might help SharePoint Online users having such a question it certainly does not help me running an on-premises environment. – Vinz Aug 1 '16 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.