Multiple queries, same page - how-to RRS feed

  • Question

  • Hi Folks -

    I have a scenario that requires, under some circumstances, that I submit a query twice.  The scenario is basically thus:

    0.  In my scenario, if the query term from the first result on page 1 is equal to an identifier (managed prop, let's call it itemid), I want
    to pass this info to a custom web part that draws attention to this fact (and displays a simple message, such as 'You've hit the jackpot.'

    1.  If a user chooses to see results from 'page 2,' I'd like to preserve awareness of this 'jackpot' state on page 1; so, I am thinking of
    submitting a second query, and grabbing only the first result.  It would be nice to be able to maintain the existence of this previous condition
    in (for example) a session object, but I have heard there are limitations and difficulties in so doing in SharePoint.   

    2.  The core results web part should only focus on results associated with the second query (ie, page 2 results) - same is true for the refiners
    and other web parts on the page.

    I'm reading a 'Tip' from the new book by Svenson, Johansson and Piddocke, and it discusses the use of a search query results ID, but no meta
    example is included.  I'm wondering if the use of this param could be a solution to what I wish to do.  And if not - could someone help me see my way
    clear to addressing this?

    THanks all -

    Paul L.

    Paul C. Loefstedt

    Saturday, April 28, 2012 2:21 PM

All replies

  • Hi Paul,

    I can't understand your goal, but here's some advice:

    1. Use URL/Server redirect and pass a URL parameter.

    2. From my experience, the parameter will be passed along to all other links of the search results page  - paging,actions and refiners, so you you'll be able to pass it along to page 2.

    3. Extend Core Results, specifically the GetXPathNavigator function. There you'll be able to process the returned results to find if your condition is met. If condition is met - redirect ! (it rhymes :)

    4. You can catch your parameter both in Core Results and in your custom webpart.

    Hope this helps.


    Sunday, April 29, 2012 6:21 AM
  • Thanks for the input, Amir.  I think my question was really about controlling the flow of the
    results emanating from a second query.  Principally, we would have the first query sending
    its results xml to all the web parts, except the one that had the promotional ("You;ve hit the
    Jackpot").  For that one, I'd want the second query's results to be consumed - but these results
    should not be sent to any of the other web parts.

    So in essence, I'm thinking it boils down to being able to add or read some kind of query identifier (query1, query2), perhaps, and use that to determine whether to consume the results from the query.

    Does this make sense?


    Paul C. Loefstedt

    Monday, April 30, 2012 2:06 PM
  • Hi Paul,

    I'm sorry, but its still unclear what you want to achieve.

    Lets see if I got you: You have a webpart that has a Boolean mode, either "You've got jackpot" (== true) or not (== false).

    If "You've got Jackpot" is true, which is determined by the first query, that webpart should do another query, process its results and do something with it. Also, it should not effect Core Results Webpart or any other webparts in the search page.

    So far, am I right?


    Tuesday, May 1, 2012 12:58 PM
  • Hi Paul,

    Sorry we didn't add a sample on query result id's, but I think it would work for you. And I assume you want to trigger the "jackpot" based on search result #1, and not on the query term as you write(?).

    You have your refiner and core results web part which show the result of the user query. Then create your own web part inheriting the CoreResultsWebPart and configure it to use Query ID 2 and only show 1 result as you want to check the top one(?). Inside that web part you have to override some methods.

    First of all you need to hook in to the query flow and set your query by using the FixedQuery property in order to pick up the user query from the k parameter.

    protected override void ConfigureDataSourceProperties()
       this.FixedQuery = Request["k"];

    Secondly you have to override GetXPathNavigator

    protected override XPathNavigator GetXPathNavigator(string viewPath)
       // Get the search results for your specified QueryID
       QueryManager queryManager = SharedQueryManager.GetInstance(Page, QueryNumber).QueryManager;
       XmlDocument xmlDocument = queryManager.GetResults(queryManager[0]);
       // Examine the xml here for "jackpot" and modify it to display what you want
       // Send the XML to the XSLT transformation
       return xmlDocument.CreateNavigator();

    When you create your custom core results webpart, make sure to add the correct default settings in order for it to work. Using one of the samples from the book and modifying it should work just fine.

    Note: I have not tested this code, but I think it should do the trick :)

    Mikael Svenson

    Search Enthusiast - SharePoint MVP/WCF4/ASP.NET4
    Author of Working with FAST Search Server 2010 for SharePoint

    Wednesday, May 2, 2012 8:18 PM
  • Michael - I never thanked you for your detail response - haven't had time to try this approach out (as I've moved on
    to other projects) but thank you!


    Paul C. Loefstedt

    Thursday, May 17, 2012 2:10 PM