Retrieve photo url for user

Posted by Community Admin on 04-Aug-2018 23:23

Retrieve photo url for user

All Replies

Posted by Community Admin on 04-Nov-2013 00:00

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


Posted by Community Admin on 05-Nov-2013 00:00

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;

You can modify this so it takes as an argument the user ID or change the return type to something else which will best suit your specific needs.

Regards,
Ivaylo Angelov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 09-Nov-2013 00:00

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;

Posted by Community Admin on 12-Nov-2013 00:00

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;
        


Regards,
Strahil Strahilov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 15-Nov-2013 00:00

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?

Posted by Community Admin on 15-Nov-2013 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed