Setting an avatar

To set the avatar of the user, you must perform the following:

  1. Get instances of the required managers.
    You must get instances of the UsersManager, UserProfileManager and LibrariesManager classes.
  2. Get instance of the user.
    Get the specified user. For more information, read Querying users.
  3. Get instance of the user profile.
    Get instance of the user profile of the specified user. For more information, read Querying user profiles.
  4. Get instance of the image.
    The image to be used as an avatar, must be located in the Sitefinity image libraries. For more information about working with image, read Managing images.
  5. Create a content link.
    To create a ContentLink object, call the CreateContentLink static method of the ContentLinksExtensions class. Pass the profile and the image as arguments.
  6. Set the Avatar property.
    Set the value of the Avatar property to the ContentLink object.
  7. Save the changes.
    Save the changes to the UserProfileManager instance.

Here is a code example:

public static void SetAvatar(Guid userId, Guid imageId)
{
    UserManager userManager = UserManager.GetManager();
    UserProfileManager profileManager = UserProfileManager.GetManager();
    LibrariesManager librariesManager = LibrariesManager.GetManager();
 
    User user = userManager.GetUser(userId);
 
    if (user != null)
    {
        SitefinityProfile profile = profileManager.GetUserProfile<SitefinityProfile>(user);
 
        if (profile != null)
        {
            Image avatarImage = librariesManager.GetImages().Where(i => i.Id == imageId).SingleOrDefault();
 
            if (avatarImage != null)
            {
                ContentLink avatarLink = ContentLinksExtensions.CreateContentLink(profile, avatarImage);
 
                profile.Avatar = avatarLink;
 
                profileManager.SaveChanges();
            }
        }
    }
}

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK