Tags

, , ,

Sitecore enables user management for the website with the Sitecore security tools in the backend. There are a few properties of the user entity that are out of the box, like ‘Portrait’.

sitecore user profile

There are occasions in which you want to show the UserProfile portrait on the website itself. For example, to show the image that the user has uploaded to his personal profile. One could use code like this:

public string GetPortraitImageUrl(Sitecore.Security.Accounts.User user)
{
	if(user == null
		|| user.Profile == null
		|| string.IsNullOrWhiteSpace(user.Profile.Portrait))
	{
		return null;
	}

	string imageUrl = string.Empty;
	string imageTypesSetting = Sitecore.Configuration.Settings.GetSetting("ImageTypes", "|jpg|img|gif|");
	string[] imageExtensions = imageTypesSetting.Split('|');

	string portraitValue = user.Profile.Portrait;

	foreach (string imageExtension in imageExtensions)
	{
		if (!string.IsNullOrWhiteSpace(imageExtension) && portraitValue.EndsWith(imageExtension))
		{
			portraitValue = portraitValue.Replace(imageExtension, "ashx");
		}
	}

	string portraitsFolder = SettingManager.GetPortraitImageFolder();

	imageUrl = string.Concat(portraitsFolder, "/", portraitValue);

	return imageUrl;
}

What this code does is the following. First, the User object must be complete. Then we have to get the possible image extensions from the Sitecore settings, and replace the image extension of the uploaded image with ‘ashx’, because that is how Sitecore makes its imageUrls. You have to find out on your own where you save your profile portrait images. I would save them somewhere in the Media Library. The implementation of how to retrieve the folder where these images are located, is also custom, so you have to find it out on your own.

Good luck, and happy coding!

Advertisements