SharePoint 2010 - MySite Redirection

When user's click a user's name in a document library, etc, a file userdisp.aspx controls where the user is directed. Assuming you want to have all user links bring users to My Sites, you can start off creating the user control, by the following code on http://blogs.sharepointguys.com/brendon/sharepoint-2007/programming/redirect-to-your-own-mysite-landing-page/ and then in the RedirectIfNecessary(SPListItem user) you can utilize the following code.

if (Request.QueryString["id"] != null)
{
    SPSite _site = SPContext.Current.Site;
    SPServiceContext _serviceContext = SPServiceContext.GetContext(_site);
    UserProfileManager _userProfileManager = 
        new UserProfileManager(_serviceContext);
    string _mySiteUrl = _userProfileManager.MySiteHostUrl;
    string _profileUrl = string.Empty;
    int userID = 0;
 
    if (int.TryParse(Request.QueryString["id"].ToString(), out userID))
    {
        SPUser profileUser = SPContext.Current.Web.SiteUsers.GetByID(userID);
        if (_userProfileManager.UserExists(profileUser.LoginName))
        {
            UserProfile _userProfile = 
                _userProfileManager.GetUserProfile(profileUser.LoginName);
            _profileUrl = _userProfile.PublicUrl.AbsoluteUri;
        }
        else
        {
            _profileUrl = String.Format("{0}Person.aspx?accountname={1}", 
                _mySiteUrl, profileUser.LoginName);
        }
 
        SPUtility.Redirect(_profileUrl, SPRedirectFlags.Static, this.Context);
    }
}
Post a Comment

Popular posts from this blog

C# - ListView Item Spacing (Padding)

IIS / ASP.NET - Run all Managed Modules for All Requests (RAMMFAR)

IIS / ASP.NET - Disabling Compatibility Mode/View (Internet Explorer)