SharePoint Online. Getting User Picture URL
I've posted about document thumbnail in SharePoint Online. And today I'll show you how you can get a picture of a user.
Samples in this post work not only SharePoint Online but SharePoint on-premise (at least SharePoint 2019 and SharePoint 2016).
As for document thumbnail, there is the base URL for user pictures in SharePoint:
To identify the user there are two query string parameters:
The user picture is stored in the user profile. So SharePoint needs to reach the profile. If AccountName parameter is presented in the response, SharePoint will try to find out the profile using that value (it may be primary account name or email).
If you provide UserName parameter, SharePoint will try to resolve this identity and reach the associated user profile.
Therefore SharePoint always gets user image according to AccountName parameter.
Provide AccountName parameter instead of UserName one, if it's possible, to avoid unnecessary operations on the server-side.
Getting image by UserName:
The more effective approach (if you know primary e-mail or account name of the user):
There are three possible sizes of user picture in SharePoint:
- L - 240x240
- M - 72x72
- S - 48x48 (default value)
For size, you need to use Size parameter.
Getting user image sized 240x240px by AccountName:
By default SharePoint caches picture for one day. Using Cache and t you can manage this behavior.
Cache parameter can be any value and t one - number of seconds.
Getting user picture with caching for 5 minutes (300 seconds):
Cache options available only for SharePoint on-premise instances. SharePoint Online always caches profile images for 24 hours.
To manage cache settings use DisableEnhancedBrowserCachingForUserPhotos property for your web application. The following PowerShell script turn on cache options for user profile image:
$webApp = Get-SPWebApplication https://sharepointportal $webApp.Properties.Add("DisableEnhancedBrowserCachingForUserPhotos", "true") $webApp.Update()