Home > SharePoint, SharePoint 2010 > SharePoint 2010: How to get the My Site Host URL or the Current User’s Personal Site URL Programmatically

SharePoint 2010: How to get the My Site Host URL or the Current User’s Personal Site URL Programmatically


Following on from my previous post on How to get the Content Type Hub URL programmatically, here I will show how to get the My Site Host URL as well as the current user’s Personal Site URL programmatically.

To get the My Site Host URL, you can get this via the MySiteHostUrl property of the UserProfileManager class as follows:

public string GetMySiteHostUrl(SPSite site)
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager upm = new UserProfileManager(context);
return upm.MySiteHostUrl;
}

This returns a URL as follows:
http://mysite:80/

To get a Personal Site URL, you can get this from the PersonalUrl property of the User Profile as follows:

public string GetPersonalSiteUrl(SPSite site)
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager upm = new UserProfileManager(context);
UserProfile profile = upm.GetUserProfile(@"DOMAIN\username");
return profile.PersonalUrl.AbsoluteUri;
}

This returns a URL as follows:

http://mysite/personal/username/

To get the Public Profile Page URL for a user, you can get this from the PublicUrl property of the User Profile as follows:

public string GetPersonalSitePublicUrl(SPSite site)
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager upm = new UserProfileManager(context);
UserProfile profile = upm.GetUserProfile(@"DOMAIN\username");
return profile.PublicUrl.AbsoluteUri;
}

This returns a URL as follows:

http://mysite/Person.aspx?accountname=domain%5Cusername

My thanks to Richie H’s blog for pointing me in the right direction.

Advertisements
  1. Ross
    September 27, 2013 at 14:14

    Is this the same for SP 2013 as well?

  1. September 27, 2013 at 20:54

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: