Whose My Site am I on? (Programmatically)

748 views Asked by At

I am building a web part to put on Sharepoint My Sites. I need to get the SPUser whose My Site the web part is on. Currently I simply use

Request.QueryString["accountname"]

but this will not work on my own My Site, and I am not sure it will work all the time either.

3

There are 3 answers

3
Daniel On BEST ANSWER

When Request.QueryString["accountname"] is empty the user should be on its own mysite so then you could look in SPContext.Current.Web.CurrentUser to get the user.

1
Nacht On

Another possible way of doing this is to use the SPSite's Owner property. This will give you an SPUser object which is usually preferable. This property correlates to the "Site Owner" property which can be configured in Central Admin under "Change Site Collection Administrator". However be aware that since this can be configured, it is not to be trusted as an absolute source of knowing whose my site you're on.

1
luccio On

here is another approach using UserProfile (Microsoft.Office.Server.UserProfiles)

var profileLoader =   Microsoft.SharePoint.Portal.WebControls.ProfilePropertyLoader.FindLoader(HttpContext.Current.Handler as Page);
var userProfile = profileLoader.ProfileLoaded;

var loginName = userProfile["AccountName"];

And then just get your SPUser from SPContext.Current.Web;