3

Let's say that we have the SPUser object of current user - how do I programmatically retrieve his mysite URL path, for example "mydomain:8080/personal/saltarski"?

Currently I do it through string manipulation:

var loginName = SPContext.Current.Web.CurrentUser.LoginName.Substring(SPContext.Current.Web.CurrentUser.LoginName.IndexOf('\\') + 1);
var hostName = SPContext.Current.Site.HostName;
SPSite site = new SPSite("http://" + hostName + ":8080/personal/" + loginName + "/")

This works for most cases, however I find that it breaks for some users like the System Admin, when his loginName resolves to 'system' but the actual path to the mysite contains his actual credentials' login name.

Is there a more elegant solution to what I am doing?

11

UserProfile.PersonalSite property gets the personal site of the User:

var profileManager = new UserProfileManager(ServerContext.GetContext(SPContext.Current.Site));
var profile = profileManager.GetUserProfile("domain\\username");
using (SPSite personalSite = profile.PersonalSite)
{
     var personalSiteUrl = personalSite.PersonalUrl; 
}
2
using (SPSite site = new SPSite(userProfile.PersonalSite.ID, this.Site.Zone))
{
   string redirectURL = SPUrlUtility.CombineUrl(site.Url, SourceUrl);
   CommonHelper.RedirectToPage(redirectURL, false, false, this.Context);
}

Do not forget about Zone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.