Extending SharePoint people search web part to be more social

People search is a NOT a new feature in SharePoint 2010. When you create an enterprise search site, you have the ability to search for individuals in your organization and see results as shown below:

In my book we looked at various options on how to manage the search results in your solution. In this article, I would like to show you how you can extend CoreResultsWebPart to display a list of site users who belong to the same department as currently logged in user. Easy way to get to know your co-workers :)

I assume you’re using existing Enterprise Search Site, in my case the URL is: http://intranet.contoso.com/search/

1. Navigate to the http://intranet.contoso.com/search/Pages/results.aspx, which is results page of the site. Access the edit-mode of the page; notice in the top-right hand side corner we have People Matches web part:

… this is the idea we’d like to transfer to any other page on the site and display users from the same department which current user belongs to.

1. Create blank Visual Studio 2010 project and specify the URL of your intranet site, http://intranet.contoso.com as a debug URL

2. Ensure you have added te following references to your project:

3. Add a new web part to the project (NOT Visual Web Part) and replace the code behind of the web part, in my case WebPart1.cs with the following:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server.Search.WebControls;
using System.Collections.Generic;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Taxonomy;
namespace SharePointProject2.WebPart1
{
[ToolboxItemAttribute(false)]
public class WebPart1 : CoreResultsWebPart
{
protected override void ConfigureDataSourceProperties()
{
    this.FixedQuery = GetQuery();
    base.ConfigureDataSourceProperties();
}
private string GetQuery()
{
    return GetDepartment(SPContext.Current.Site.Url);
}
public string GetDepartment(string socialDataStatsSite)
{
    string department= string.Empty;
    using (SPSite siteColl = new SPSite(socialDataStatsSite))
    {
        SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
        UserProfile userProfile = ProfileLoader.GetProfileLoader().GetUserProfile();
        if (userProfile[PropertyConstants.Department] != null &&
userProfile[PropertyConstants.Department].Value !=null)
        {
            department = userProfile[PropertyConstants.Department].Value.ToString();
        }
    }
    return department;
}
}
}

In here, we extend CoreResultsWebPart where we get a hold of the currently logged in user profile property – Department, and pass it as a Fixed Query value to our search results web part. As a result, the web part will always execute a search query of the currently logged in user’s department.

4. Deploy your solution to the site from Visual Studio.

5. Navigate to the site, in my case http://intranet.contoso.com and edit the page. If your deployment was successful, you will be able to add a new web part to any of the web part zones. In my case, the web part is deployed to Custom category and is called WebPart1.

6. When you add a web part, you need to configure few other properties. First, you need to specify which content source to use to perform your search. In our case it’s people. Edit newly added web part and specify Location Properties to equal Local People Search Results as shown below:

7. The last item, is to make web part rendering more like below:

Navigate to the http://intranet.contoso.com/search/Pages/results.aspx, which is results page of the site. Access the edit-mode of the page (Site Action -> Edit Page); in the top-right hand side corner we access the properties of People Matches web part.

8. Access the Display Properties of the web part and copy the XSL by pressing the XSL Editor button shown below:

9. Paste the contents of the XSL Editor to the same place in our newly added web part and save the properties of our new extended web part.

That’s it. Now, if currently logged in user has a department property assigned to their profile – they will see their co-workers showing up just if they performed a search from the people search.

Enjoy!

This entry was posted in sharepoint 2010 and tagged , . Bookmark the permalink.

Comments are closed.