Thursday, December 29, 2011

SharePoint 2010 Custom “My” Search Core Results

I’ve had a need to return search results specific to the currently logged in user which display on page load so I developed a My Search Core Results web part which inherits from the OOTB CoreResultsWebpart. Scott Hillier's solution for returning appended "My" results was a basis for this web part. But where Scott's example code appends to the user query, this generates a dynamic fixed query so results appear on page load.

The core of this web part is the following snippet:

Code Snippet
  1. namespace WP.CustomSearch.MySearchBuilder
  2. {
  3.     [ToolboxItemAttribute(false)]
  4.  
  5.     public class MySearchBuilder : CoreResultsWebPart
  6.     {
  7.         [Personalizable(PersonalizationScope.Shared)]
  8.         [WebBrowsable(true)]
  9.         [WebDescription("Main Keyword Query to search")]
  10.         [WebDisplayName("Fixed Query to Search")]
  11.         [Category("My Query Builder")]
  12.         public string MainQueryPrefix { get; set; }
  13.  
  14.         [Personalizable(PersonalizationScope.Shared)]
  15.         [WebBrowsable(true)]
  16.         [WebDescription("Mananged Property that should be searched for current user")]
  17.         [WebDisplayName("My Query Managed Property")]
  18.         [Category("My Query Builder")]
  19.         public string MyQueryPrefix { get; set; }
  20.  
  21.         [Personalizable(PersonalizationScope.Shared)]
  22.         [WebBrowsable(true)]
  23.         [WebDescription("Sort by this managed property")]
  24.         [WebDisplayName("Managed Property")]
  25.         [Category("Sort Override")]
  26.         public string OrderByProperty { get; set; }
  27.  
  28.         [Personalizable(PersonalizationScope.Shared)]
  29.         [WebBrowsable(true)]
  30.         [WebDescription("Sort direction")]
  31.         [Category("Sort Override")]
  32.         public Microsoft.Office.Server.Search.Query.SortDirection SortDirection { get; set; }
  33.  
  34.         protected override void ConfigureDataSourceProperties()
  35.         {
  36.             string QueryFormat = MainQueryPrefix + " " + MyQueryPrefix + ":\"{0}\"";
  37.  
  38.             string currentUserName = SPContext.Current.Web.CurrentUser.LoginName;
  39.             string currentName = SPContext.Current.Web.CurrentUser.Name;
  40.  
  41.             StringBuilder sb = new StringBuilder(String.Format(QueryFormat, currentUserName));
  42.             sb.Append(String.Format(" " + MyQueryPrefix + ":\"{0}\"", currentName));
  43.  
  44.             this.FixedQuery = sb.ToString();
  45.  
  46.             // only do stuff when search results are visible
  47.             if (this.ShowSearchResults)
  48.             {
  49.                 // run the base code
  50.                 base.ConfigureDataSourceProperties();
  51.                 try
  52.                 {
  53.                     // if OrderByProperty is not set, use default behavior
  54.                     if (!string.IsNullOrEmpty(OrderByProperty))
  55.                     {
  56.                         // get the datasource and change the sortorder
  57.                         CoreResultsDatasource dataSource = this.DataSource as CoreResultsDatasource;
  58.                         dataSource.SortOrder.Clear();
  59.                         dataSource.SortOrder.Add(OrderByProperty, SortDirection);
  60.                     }
  61.                 }
  62.                 catch (Exception ex)
  63.                 {
  64.                     SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("MYWEBPART", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }

The Web Part has configuration settings for "My Query Builder" which provides the ability to add an initial fixed query value (e.g.., to return just documents you could enter "IsDocument:1" in this field) and a field where the Managed Property contains the current user (e.g. ModifiedBy, Author, or your own Managed Property) so when the web part renders it adds the fixed query (e.g IsDocument:1 ModifiedBy:"<user login>"). Note: If you want to throw more than one of this webpart on a single page, make sure to set a unique Cross-Web Part query ID.  As you can see in screenshot below, the values from “My Query Builder” dynamically populate the fixed keyword query adding the currently logged in users ID to the fixed query.

image

The full Visual Studio solution can be downloaded here.

1 comment:

  1. have you tried using your web part in an environment where FAST is configured. I have been using it and it is working well in a non fast environment but it returns no results when i try it in the FAST enabled environment. The OOTB core rsults query works fine but yours does not

    Kevin

    ReplyDelete