Last post we covered how to add a core search results web part to a SharePoint page and configure the Web Part to show the results from our custom search scope which we created in part 1. If you didn’t read these posts, they can be found here:

Part 1: Creating Custom Search Scope to only find tasks from any SharePoint Site or Farm.

Part 2: Configuring the Search Web Part to use the custom search scope and display the results and details that we want

Today we will cover Part 3 and finally wrap up this exercise. First we use SharePoint Designer to change the XSL styling to get rid of the ugly search result look and give us a more traditional list look and feel. Then we go through exporting the Web Part. Finally we dive into visual studio to do some custom coding and make it so that the only results displayed by this Web Part are tasks assigned to the current user.

So lets get started. The first step here is optional, you only have to do this if you want some real data to work with in the data view. I highly recommend it since it allows you to see your work and results in real-time rather then in your head but hey its up to you. Open up the properties of the search core results web part that we have been working with and in the XSL editor add this code. What it will do is return all of our search results in XML format, which we can save to a file and use as a data source in our next step.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xmp><xsl:copy-of select="*"/></xmp>
</xsl:template>
</xsl:stylesheet>

Apply the changes and now in our search results web part we get our search results as XML. The content of your core search results web part should look something like this:

Search Results as XML

Copy it all and paste it into a file and save as results.xml. Now open up SharePoint Designer 2010 and go to the site that has the web part we have been working with. Click on Data sources on the left hand side. Then we are going to create a new data source from XML file. So Click on XML File Connection button in the ribbon.

 SharePoint Designer

When prompted navigate to and select the results.xml file we saved earlier. Add the file to the site and click ok. You now have a new data source to work with. Open up the page that has the search web part on it in edit mode. Select an area that you can insert a web part in and then click Insert –> Data View –> Results.xml

SharePoint Designer Data View

This is the quick way to get results showing. I find it a little easier to actually insert an Empty Data View and then link the data source and pick which fields you want to display in which order. Either way is fine. Both ways require that you know go and choose what fields you want to display from the right hand side. I selected Title first, then description, then task status, and finally assignedTo (although they should all be assigned to me, this will prove if its working or not). I insert as a multiple item view and here we go. Now that you have all the fields you want you can move items around in the design window to get it showing the way you want. After you have everything set, we want to go into the web part properties and then the XSL editor and copy all of the code, then go into the properties of the search web part and the xsl editor and replace everything in there with the code from the data view. Yes I know surprisingly this just works!

Save, stop editing and view it in a browser if it is working and looking the way you want (except showing results for all users) that’s great keep moving on. If not go back and fix it because you won’t do it later.

Now lets export this web part so that we can reuse the work that we have already done to build our reusable solution for all users. Go to the page that we have our core search results web part on that we have already configured to use our custom search scope, display some additional fields, and have just changed the styling. You click on the drop down arrow in the top right corner of your web part and then select export. Save this web part to somewhere you’ll remember.

Now open up visual studio and create a new SharePoint Empty Project. For this example we will work in C#. Then add a new item to the project and choose Web Part. Right click on your new item and click add and then existing item and select the web part that we exported earlier. Go ahead and delete the other .webpart file that was created by default or you can just replace the contents. Ensure that the name of the .cs file matches and that all references in the .cs and .xml file are updated accordingly. At this point your project should look something like this:

Visual Studio SharePoint WebPart

Open up the .webpart file as we need to make one change in it. We need to change metadata type element to load the MyTasksWebPart code (which we create in a minute) rather than the CoreResultsWebPart code.

We started with this:

Visual Studio webpart metadata element

And changed to this:

visual studio custom sharepoint webpart

Now we open up the .cs file and make the necessary changes to show only tasks assigned to the current user. So double click on the MyTasksWebPart.cs

If you have used all of the same names then you can just replace the contents of that file with the below code.

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.Office.Server.Search;

using Microsoft.Office.Server.Search.Query;

using Microsoft.Office.Server.Search.WebControls;

namespace MyTasksRollup.MyTasksWebPart

{

    [ToolboxItemAttribute(false)]

    public class MyTasksWebPart : Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart

    {

        QueryManager _queryManager;

          protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

            _queryManager = SharedQueryManager.GetInstance(this.Page).QueryManager;

        }

        protected override System.Xml.XPath.XPathNavigator GetXPathNavigator(string viewPath)

        {

            SPUser user = SPContext.Current.Web.CurrentUser;

            _queryManager.UserQuery = string.Format("scope:"Tasks" AssignedTo:"{0}"", user.Name);

            return base.GetXPathNavigator(viewPath);

        }

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            //debug info

            //Controls.Add(new Label { Text = string.Format("FixedQuery: {0}<br/>AppendedQuery: {1}<br/>UserQuery: {2}", FixedQuery, AppendedQuery, _queryManager.UserQuery) });

        }

    }

}

If you have been using different names, or want to understand what we did, here is the explanation:

First our code looked like this:

namespace MyTasksRollup.MyTasksWebPart

{

    [ToolboxItemAttribute(false)]

    public class MyTasksWebPart : WebPart

    {

        protected override void CreateChildControls()

        {

        }

    }

}

The first thing we do is make the new Web Part class inherit from CoreSearchResultsWebPart which is in the Microsoft.Office.Server.Search assembly. To do this we changed this line

public class MyTasksWebPart : WebPart

To this:

public class MyTasksWebPart : Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart

Then we override a few methods so that we can add the filtering that we need through the SharedQueryManager for the page. This is done with each line that you see “protected override” on. The real magic in this code is the code in the GetXPathNavigator. This is where we actually filter for the current user in the AssignedTo field

protected override System.Xml.XPath.XPathNavigator GetXPathNavigator(string viewPath)

        {

            SPUser user = SPContext.Current.Web.CurrentUser;

            _queryManager.UserQuery = string.Format("scope:"Tasks" AssignedTo:"{0}"", user.Name);

            return base.GetXPathNavigator(viewPath);

        }

Now save it all, build and deploy. You will now have this as a web part option to add to your page. It will be grouped under custom unless you put it under a different group. Add it to the page and enjoy.

Hope you find it useful, Hope it saved you some time and headaches and as always I look forward to hearing your feedback, comments, and suggestions.

 

Here’s what mine look like:

SharePoint content rollup from anywhere