How to get the users image in my site programatically?
I need to show image of the logged-in user in my webpart.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityHow to get the users image in my site programatically?
I need to show image of the logged-in user in my webpart.
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];
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;