MOSS2007 – Using the Search API (Part 3)

转自:http://www.thesug.org/Blogs/helotslim/archive/2009/5/6/MOSS2007–UsingtheSearchAPI(Part3).aspx

 

In the last few posts we have looked at some of the Search API basics. One of the issues I mentioned earlier was that the "PublishingRollupImage" does not show itself in the search results. Using the following XSL code you can see this:

I decided to use the Search Coder tool and see if this would display the "PublishingRollupImage". I ran the same search query as before and the following was returned:

However I noticed that the required field did not return any data again.

As you can see this is obviously an issue with the core search API. So I set about trying to fix this issue and the idea was to use the code we created last time (which is a CAML query) and somehow use it inside the standard Search Results Web Part. So the architectural idea is as follows:

  1. Create new Web Part that inherits from the Search Core Results Web Part
  2. Create a new XSLT for the Results
  3. Create a custom XSLT Extension

So let's create the web part. Firstly open up Visual Studio and choose to create a web part project. Make sure you inherit from the following:

Based on our design, we only need to override the following method called "ModifyXsltArgumentList":

We need to add a reference to our custom XSLT extension that we have not added yet.

So now we have our web part done lets add the XSLT extension code. This for ease and convenience will be added to our web part as a new class. Not the best approach but for this demonstration should be fine.

The code is more or less the same as the previous post so does not need documenting here. J

I was going to create a new XSLT but thought it may be easier to use one of the ones that exist here:

http://www.codeplex.com/sctxsl

I chose to use the following XSLT: http://sctxsl.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=12070#DownloadId=32021

So now let's tie it all together. We will add our new web part to the search page as shown below and add the XSL we used earlier to return the raw XML:

We need to modify the fields list so it includes the "PublishingRollupImage". To do this we need to modify the "Selected Columns" that are listed within the "Results Query Options".

We also need to modify the XSLT by pasting the one that we downloaded from Codeplex. We also need to modify the following lines:

Notice that we are adding references to the new XSLT Extension and calling it for our item URL. Once this is all put together it renders as shown below:

This now means that I can custom style my results anyway I wish and know the "PublishingRollupImage" will display. And this works because we are using a custom XSLT Extension class. This is a great way of solving the "PublishingRollupImage" problem, it has not been tested with lots of results but should function fairly ok as it is only called if it needs to be called. J

 

posted on 2009-07-16 14:57  王丹小筑  阅读(159)  评论(0)    收藏  举报

导航