[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.

Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>