Programmatically Determining Whether a SharePoint Page Belongs to the MySite

How do you determine programmatically whether a SharePoint page belongs to the MySite? I mean not belonging to a specific users’s MySite but to any MySite. Well, it’s actually quite easy:

// Needs references to Microsoft.Office.Server.dll 
// and Microsoft.Office.Server.UserProfiles.dll
...
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
...
UserProfileManager upm = new UserProfileManager(
 SPServiceContext.GetContext(SPContext.Current.Site));
// Remove default port from MySiteHostUrl
Uri u = new Uri(upm.MySiteHostUrl); 
bool isMySite = HttpContext.Current.Request.Url.AbsoluteUri.StartsWith(
 u.ToSTring());

But while rifling the Web for ready-made solutions I found some really involved approaches to determine the MySite host’s URL. For example checking all the RootWeb’s templates or using reflection. So posting this may save someone some time.

If you need a specific user’s MySite URL you can add this code–to keep things interesting UserProfile.PublicUrl.AbsoluteUri doesn’t include the default port:

...
UserProfile up = upm.GetUserProfile("my-domain\\his.account");
string hisMySiteURL = up.PublicUrl.AbsoluteUri;
Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s