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.
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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With