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 = "[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.

2
  • 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
    Commented May 18, 2015 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
    Commented May 18, 2015 at 15:16

2 Answers 2

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, "[email protected]", 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

1
  • 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
    Commented May 18, 2015 at 15:07
0

Please try this url, https://outlook.office365.com/owa/service.svc/s/[email protected]&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
  • 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
    Commented Aug 1, 2016 at 4:12
  • using above url ,If user don't have profile image it returning empty.So how we can check if user don't have image with this above url then how we can set the sharepoint default image in react spfx
    – Elina
    Commented May 15, 2020 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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