Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whose My Site am I on? (Programmatically)

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.

like image 788
Nacht Avatar asked Oct 12 '11 04:10

Nacht


3 Answers

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.

like image 155
Daniel Avatar answered Oct 19 '22 00:10

Daniel


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;

like image 2
luccio Avatar answered Oct 18 '22 23:10

luccio


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.

like image 1
Nacht Avatar answered Oct 18 '22 23:10

Nacht