How To: Change the PictureUrl Property of All User Profiles in a Site Collection
Applies to: SharePoint Server 2010
The code sample in this topic demonstrates how to change the value of the PictureUrl property for every user in a site collection so that it corresponds to a new location or URL for user profile photos.
Using Code to Change the PictureUrl Property for All Users
When you move the location of your My Site website host from one location to another, you will also likely move the location of your My Site photos. If you move the location of your photos, you must change the PictureUrl property for every UserProfile object in your site collection so that it corresponds to the new location. This is necessary when the URL for the new location differs from the previous URL, or if you start using the Secure Socket Layer (SSL) protocol and you have to change your image URLs so that they conform to this protocol (so that they begin with https:// instead of http://). The following code sample shows how to change the PictureUrl property for every UserProfile object in a site collection. The site collection should correspond to your My Site host. This topic assumes that you are building on Microsoft .NET Framework 3.5 and that you have added the following references to your Microsoft Visual Studio 2010 project:
Microsoft.SharePoint
Microsoft.Office.Server.UserProfiles
using (SPSite site = new SPSite("siteUrl"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager myUserProfileManager = new UserProfileManager(context);
try
{
foreach (UserProfile aUser in myUserProfileManager)
{
string origUrl = (string)aUser[PropertyConstants.PictureUrl].Value;
string newUrl = origUrl.Replace("http://mysite", "https://mysite");
aUser[PropertyConstants.PictureUrl].Value = newUrl;
aUser.Commit();
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
See Also
Reference
Microsoft.Office.Server.UserProfiles