2

How to get the users image in my site programatically?

I need to show image of the logged-in user in my webpart.

2 Answers 2

7

The function below will get you the UserProfile based on the accountName

public static UserProfile GetUserInfo(string AccountName)
{
    UserProfile profile = null;
    SPServiceContext serviceContext = SPServiceContext.Current;
    UserProfileManager profileManager = new UserProfileManager(serviceContext);
    if (AccountName != string.Empty)
    {
        profile = profileManager.GetUserProfile(AccountName);
    }
    else
    {
        profile = profileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.RawSid);
    }
    return profile;
}

The picture URL will be in

userProfile[PropertyConstants.PictureUrl];
1
  • Can I set Picture of User Profile without configuring mySite ? Commented Apr 20, 2016 at 10:42
0
  SPFieldUrlValue fieldUrl = new SPFieldUrlValue(item["Links"].ToString());
  url = fieldUrl.Url;
  string[] splittedValues = url.Split('=');
  string accountName =splittedValues[1].ToString();
  accountName.Replace("%5C","\\");

  ServerContext ServerContext = ServerContext.GetContext(site);
  UserProfileManager profileManager = new UserProfileManager(ServerContext);
  UserProfile spUser = profileManager.GetUserProfile(accountName);
  IDictionaryEnumerator userProperties = spUser.GetEnumerator();

  string picture = spUser["PictureURL"].Value.ToString();
  row["Links"] = url;
  row["URL"] = picture;
1
  • 1
    Do you always have the user account on the url? Why split strings? If you want info on current user, wouldn't it be cheaper to just get user info from SPContext.Current.Web.CurrentUser? Commented Oct 6, 2011 at 0:40

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.