Tag Archives: UserProfile

[Sharepoint] How to get the MySiteHost Url with code

Sometimes we need to reference the MySiteHost, to create a link or for any other reason.

To avoid hard-coding it or saving it on a resource file (which would need redeployment to change), we can use the UserProfileManager and retrieve it dynamically:

 public static string GetMySiteHostUrl()
     SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Web.Site);
     UserProfileManager upm = new UserProfileManager(context);
     return upm.MySiteHostUrl;

Note: It’s necessary to reference the Microsoft.Office.Server.UserProfiles Asssembly.