+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Retrieve photo url for user

Retrieve photo url for user

6 posts, 0 answered
  1. Scott
    Scott avatar
    11 posts
    Registered:
    17 Oct 2008
    04 Nov 2013
    Link to this post
    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


  2. Ivaylo Angelov
    Ivaylo Angelov avatar
    68 posts
    Registered:
    27 Oct 2016
    05 Nov 2013
    Link to this post
    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
  3. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    09 Nov 2013
    Link to this post
    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;
  4. Strahil Strahilov
    Strahil Strahilov avatar
    24 posts
    Registered:
    06 Nov 2014
    12 Nov 2013
    Link to this post
    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
  5. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    15 Nov 2013 in reply to Strahil Strahilov
    Link to this post
    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?
  6. Strahil Strahilov
    Strahil Strahilov avatar
    24 posts
    Registered:
    06 Nov 2014
    15 Nov 2013
    Link to this post
    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
6 posts, 0 answered