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="http://schemas.microsoft.com/sharepoint/"
         Id="11E746AC-377D-4a5c-8B1D-46D67FF3B25E"
         Title="Custom User Profile Redirection Control"
         ActivateOnDefault="FALSE"
         Description="Registers the delegate control to redirect user to custom Profile page."
         Scope="Farm"
         Hidden="False"
         Version="1.0.0.0">
 
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
 
</Feature>

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 
  <!-- set redirect to the new profile page on pages such as userdisp.aspx -->
  <Control Id="ProfileRedirection"
           Sequence="50"
           ControlAssembly="ProfileRedirectionTest.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"
           ControlClass="ProfileRedirectionTest.WebParts.UserProfileRedirect"/>
</Elements>

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)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    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);
                                       }
                                        else
                                        {
                                            // 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