Retrieve photo url for user
I need to retrieve the url for the user profile photo.
This is what I have so far
//Get an array of strings of the usernames in the role we choose
RoleManager roleMngr = RoleManager.GetManager("AppRoles");
var users = roleMngr.GetUsersInRole("Staff");
UserProfileManager pb = UserProfileManager.GetManager();
var profile = pb.GetUserProfile(user.Id, typeof(SitefinityProfile).FullName);
var photo = Telerik.Sitefinity.Model.DataExtensions.GetValue(profile, "Avatar");
But i have figured out how to get to the contentlink
I will be looping through the users and displaying a user directory
Hello Scott,
To get the URL of the user's Avatar, you can use the following method:
private
string
GetUserAvatarLink(
string
username)
UserManager userManager = UserManager.GetManager();
UserProfileManager profileManager = UserProfileManager.GetManager();
LibrariesManager librariesManager = LibrariesManager.GetManager();
User user = userManager.GetUsers().Where(us => us.UserName.Equals(username)).FirstOrDefault();
string
result =
string
.Empty;
if
(user !=
null
)
SitefinityProfile profile = profileManager.GetUserProfile<SitefinityProfile>(user);
if
(profile !=
null
)
if
(profile.Avatar !=
null
)
ContentLink avatarLink = profile.Avatar;
var imageId = avatarLink.ChildItemId;
Telerik.Sitefinity.Libraries.Model.Image avatar = librariesManager.GetImage(imageId);
result = avatar.Url;
return
result;
Be aware this line will cause you potential yellow screens if the image is deleted, or non-published image URLs which you rarely want to display anywhere:
avatar = librariesManager.GetImage(imageId)
Also, not 100% but I think image Urls are .MediaUrl not .Url ?
instead use (sorry no compiler handy so check this yourself):
var avatar = librariesManager.GetImages().SingleOrDefault(o => o.Id == imageId && o.Status == Live && o.Visible);
result = avatar == null ? string.Empty : avatar.MediaUrl;
Hi Scott,
This is how you can get your user avatar URL or avatar thumbnail URL:
using UserProfilesHelper class:
private
string
GetUserAvatarUrl(Guid userId)
Telerik.Sitefinity.Libraries.Model.Image image;
var imageUrl = UserProfilesHelper.GetAvatarImageUrl(userId,
out
image);
return
imageUrl;
private
string
GetUserAvatarThumbnailUrl(Guid userId)
Telerik.Sitefinity.Libraries.Model.Image image;
string
thumbnailUrl = String.Empty;
var imageUrl = UserProfilesHelper.GetAvatarImageUrl(userId,
out
image);
if
(image !=
null
&& !
string
.IsNullOrEmpty(image.MediaUrl))
thumbnailUrl = image.ThumbnailUrl;
return
thumbnailUrl;
Thank you Strahil - this is a great looking helper class! The more of these Sitefinity has the better for all of us!
Do you know of any others?
Hello Stephen,
I advice you to check our documentation portal or Api Reference documentation.
You can find very useful information there.
Thank you for choosing Sitefinity CMS.
Regards,
Strahil Strahilov
Telerik