Question

In a Sharepoint WebPart written in c# I need to find out either the ID of the current users MySite, or check if the current site is the users MySite.

Ideas?

Was it helpful?

Solution

I have spent the afternoon working on this and have worked it out.

Add the following using statements after referencing Microsoft.Office.Server.dll and Microsoft.Sharepoint.dll

using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

Then access the user profile by doing:

ServerContext sc = ServerContext.Current;
UserProfileManager upm = new UserProfileManager(sc);
UserProfile up = upm.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);

Then you can get the Site Collection ID (SPSite.ID) of the MySite via:

upm.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName).ID

Or the Site ID (SPWeb.ID) via:

upm.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName).PersonalSite.RootWeb.ID

Obvious, well not really!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top