Search This Blog

Monday, November 21, 2011

Redirecting to a custom user profile page:SharePoint 2007

By default, whenever we click on a user name like <DomainName>\UserName in a sharepoint site, it redirects us to the sharepoint user profile page  i.e.'/_layouts/userdisp.aspx'which in turn redirects to the page in 'MySite' which displays the user details.

Many a times, we come across a requirement where in we need to redirect the user to a custom user profile page which might contain any other business specific information for the user as well as a picture and other details etc.This page can reside in the 'Pages' library of the site or in the 'layouts' folder in the 12 hive.

In order to do this, we can use a feature to substitute our own webpart in the delegate control already present on the userdisp.aspx page.

This delegate control has the controlid 'ProfileRedirection' with the scope = "Farm".

Therefore, the feature that we develop will have to be a 'Farm' scoped feature.If we need to selectively enable this behaviour for only a particular site,we can filter for that site in the webpart code (as is shown below).

The feature.xml, elements.xml as well as the webpart code is given below:

<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns=""
         Title="Custom User Profile Redirection Control"
         Description="Registers the delegate control to redirect user to custom Profile page."
    <ElementManifest Location="elements.xml"/>

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="">
  <!-- set redirect to the new profile page on pages such as userdisp.aspx -->
  <Control Id="ProfileRedirection"
           ControlAssembly="ProfileRedirectionTest.WebParts, Version=, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"

The webpart code is as follows:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace ProfileRedirectionTest.WebParts
    public class UserProfileRedirect:SPControl
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                        using (SPWeb web = site.RootWeb)
                            string sourceUrl = string.Empty;

                            if (HttpContext.Current.Request.QueryString["Source"] != null)
                                sourceUrl = HttpContext.Current.Request.QueryString["Source"].ToString();
                            if (HttpContext.Current.Request.QueryString["ID"] != null)
                                SPUser spUser = web.AllUsers.GetByID(int.Parse(HttpContext.Current.Request.QueryString["id"]));
                                ServerContext cntx = ServerContext.GetContext(site);
                                UserProfileManager profileManager = new UserProfileManager(cntx);
                                if (profileManager != null)
                                    bool userExists = profileManager.UserExists(spUser.LoginName.ToString());
                                    if (userExists)
                                      if (web.Title.Equals("My Test Site"))
                                            string userProfileUrl = "/_layouts/TestProject/ViewUserProfileFull.aspx?" + "un=" + spUser.LoginName.ToString() + "&source=" + Microsoft.SharePoint.Utilities.SPHttpUtility.HtmlEncode(sourceUrl);
                                            //to redirect to a custom page
                                            HttpContext.Current.Response.Redirect(userProfileUrl, true);
                                            // to redirect to my site page
                                            HttpContext.Current.Response.Redirect(profileManager.MySiteHostUrl + "/Person.aspx?accountname=" + spUser.LoginName.ToString() + "&Source=" + sourceUrl);

            catch(Exception ex)
               throw ex;


No comments:

Post a Comment