Improved Core result webpart   

Tags: SharePoint 2010, Taxonomy, Search
Technorati Tags: , ,

In my last blog post about extending the core result webpart “Multilingual Taxonomy Search” i explained that searching for multilingual taxonomy terms didn’t work how it should work.

In this blog post I’m going to rewrite the code I used for extending the webpart. The code that is used in the post works perfectly with the “Microsoft.SharePoint.Search.Internal.WebControls” assembly. The assembly that we are going to use is “Microsoft.Office.Server.Search.WebControls”.

 

The Code

Step 1: Create a new empty SharePoint project and name ImprovedCoreResultWebPart

 

Step 2: Add a new webpart in this case we are using the default webpart and name it e.g. SearchResultQuerWP.

WebPart

 

Step 3: Add the following assembly “Microsoft.Office.Server.Search.WebControls”, path: “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Search.dll”

Using Microsoft.Office.Search.WebControls;

 

Step 4: Extend the webpart

public class SearchResultQueryWP : CoreResultsWebPart

 

Step 5: Add a query manager

QueryManager _queryManager;

 

Step 6: Override the OnInit method

protected override void OnInit(EventArgs e)
{
     base.OnInit(e);
     _queryManager = SharedQueryManager.GetInstance(this.Page).QueryManager;
}

 

Step 7: Create the method that will split the keywords that the user typed in

private string SetQuery(string query)
{
 
    //split the search keywords 
      string[] words = query.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
 
     //string oldword = string.Empty;
 
     string id = string.Empty;
 
     //the new query that's going to be send back
            string newQuery = query;
 
    //loop each word to get the id of the taxonomy term
     foreach (string word in words)
     {
          id = GetTermId(word);
              
          newQuery.Replace(word, id);
       }
 
       return newQuery;
 
}

 

Step 8: Create the method that will search for the taxonomy id’s

private string GetTermId(string termName, string groupName)
{
    string result = termName;
 
    //create the taxonomy session
     TaxonomySession session = new TaxonomySession(SPContext.Current.Site, false);
 
     //search the taxonomy term based on the language that sharepoint is currently in
     var terms = session.GetTermsInWorkingLocale(termName, false, StringMatchOption.ExactMatch, 1, true, true);
 
      //send the first term we found back
       if (terms.Count > 0)
       {
            result = terms[0].Id.ToString();
        }
 
        //if no term was found send back the original name
        return result;
        }

 

Step 9: Override the GetXpathNavigator to alter the keywords

protected override XPathNavigator GetXPathNavigator(string viewPath)
{
        try
        {
 
                string newQuery = SetQuery(_queryManager.UserQuery);
 
                _queryManager.UserQuery = newQuery;
 
            }
            catch (Exception ex)
            {
                this.Controls.Add(new LiteralControl(ex.ToString()));
            }
 
        return base.GetXPathNavigator(viewPath);
        }

 

Step 10: Build and deploy the solution

 

Codeplex

This project can be found (later on this week) along other ventigrate projects on our project site.

http://ventigrate.codeplex.com

 
Posted by  Gilissen Timmy  on  8/8/2011
0  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:        
 

Links to this post

Comments

Name *:
URL:
Email:
Comments:


CAPTCHA Image Validation