search blog
most popular
MCP MCTS MCT MVP

MOSS 2007: Customize the Search Result (using XSLT) - Part 3 - Customize using SharePoint Designer 2007 

Author: Tobias Zimmergren
Url:
http://www.zimmergren.net

After being rather busy the last couple of weeks due to taking the SharePoint Certifications as well as tutoring some MOSS 2007 Development around the country it has finally begun to lighten up again. Being the geeky nerd that I am (Yeah, like you aint?), of course the first thing that comes to my mind is to complete the two articles about customizing the Search Results Page with a third post on the subject which will show you a trick to use SharePoint Designer to generate your custom XSLT. These posts are by no means any in-depth articles on the subject, but rather a pointer to get you started on the journey. If there's interest to create a more in-depth article series about the subject, don't hesitate to contact me using the contactlink from my blog or leave a comment to this post.

Please refer to the previous articles if you have any questions about editing the xslt for your Search Result. They can be found here:
MOSS 2007: Customize the Search Result using XSLT
MOSS 2007: Customizing the Search Results Page (XSLT) - Part 2 - Add highlighting

Step by step

First of all, take a look at this picture to understand how it looks before the changes are done (this picture illustrates a standard-layout of the Search Core Results WebPart that is shown when you perform a search in the Search Center) 
searchresultsthree1

Modify the XSLT of the Search Core Results Web Part to get the raw XML

The search results are actually based on plain XML (which is why we can use XSLT to customize the appearance of the results to a high extent) so what we will do here is to basically use standard XSLT to get ALL the XML elements displayed in the search results as raw XML. This is done because we will use this raw XML in SharePoint Designer to generate some good looking XSLT markup for our Search Core Results Web Part.

Replace the existing XSLT markup from the Search Core Results Web Part with the following snippet (which basically just give us the raw XML output when searching):

<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>

 searchresultsthree2

Now if you publish the page, you will see a result like the result in the following picure, which illustrates how the raw XML could look: 
searchresultsthree3

Make use of the raw XML that SharePoint generated for us in the Search Core Results Web Part

Copy all the raw XML data (including the <All_Results> tags and save to a new XML-file (I named mine rawsearchresult.xml)

When you've copied down the raw XML and saved the file, launch SharePoint Designer 2007 - Let the magic begin!

 

Use SharePoint Designer 2007 to customize the XSLT for your Search Result Page in SharePoint 2007

Launch Microsoft SharePoint Designer 2007 and open up your SharePoint site (by going to "File" - "Open Site.." and enter the url to your SharePoint site (in my case, it's "http://zimmergren" and hit open)

Next, choose File - New - "ASPX" to create a new ASP.NET page. Note that you do not need to save this page because we will only use the page as a base when we customize our XSLT using SharePoint Designer. Later, when we are satisfied with the presentation of our search results, we will simply copy the generated XSLT and close the new .aspx page and ignore the changes. 
searchresultsthree4

Now from the File-meny choose "Data View" - "Insert Data View" to insert a standard DataFormWebPart.

Note, when inserting the aforementioned Data View, the Data Source Library is opened/displayed in the top right pane (if you havn't customized the appearance of SPD) as seen here: 
searchresultsthree5

In this newly opened Data Source Library we will expand the node that says "XML Files" and choose "Add an XML file...". Browse to your XML file that contains the raw XML and hit okay. 
searchresultsthree6

You will be confronted with a question asking you to import the file into SharePoint, press Ok. When you've hit OK until you're back on the aspx-page, you will see your newly added XML file under the "XML Files" node in the tree in the Data Source Library and in my case it's called "rawsearchresults.xml"
searchresultsthree7

Click the dropdown of your file, and choose "Show Data". Next, choose the tags/entities that you want to include in the presentation of your Search Result by holding down Ctrl + clicking the ones you'd like to insert. 
searchresultsthree8

Simply drag and drop those items onto the DataFormWebPart on your ASPX-page and it will automatically render the XSLT for you, like so: 
searchresultsthree9

Now, either manually edit the presentation of your search results - or choose the quick edit for your DataFormWebPart and click "Change layout..."
searchresultsthree10

On dialog, choose your desired layout to base the presentation of your search results on. 
searchresultsthree11

When we've done this, SharePoint Designer automatically renders the XSLT for us (based on the XML File we chose, that contains our raw XML for the Search Results (which is also why we get the nice preview in SharePoint Designer)) 
searchresultsthree12

Now, that might not be the best layout to use for displaying the search results on a page.. So we will have to do some manual tweaking in order to get the presentation of this XSLT to fully fit our needs and demands.

Something that is important to note is how we can e.g. change the title to become a hyperlink that links to the acutal object by choosing the title-element and using the Quick Edit to change some properties. Note that I changed the title to a Hyperlink, which links to {url} that is part of the Search Result and also setting the title to {title} which also is an element in the Search Result.
searchresultsthree13

Do all your manual tweaking (either by customizing the XSLT from the source view, or as the previous picture illustrated) and continue to the next step.

Tip: You can of course add Sorting, Filtering and Grouping aswell by simply choosing it in the same dialog that we chose to change the layout..

Use the XSLT that was rendered by SharePoint Designer 2007 in the Search Core Results Web Part

Switch to Split View or Source View, and copy the entire <xsl:stylesheet> tag and all content. 
searchresultsthree14

Now simply paste the generated XSLT into your Search Core Results WebPart's XML-section.

NOTE: If you can't see the entire Web Part and thereby not being able to access the edit-button, you can simply enter the querystring: ?contents=1 which will take you to the Web Part Maintenence Page for your site, where you can delete the webpart, and then add a new one as usual, and edit the XSLT of the new Search Core Results Web Part instead.

Publish the page, and behold your new presentation of the Search Results. Please note that I didn't make any heavy customizations to my XSLT and therefore it's rather steril, but you can make some major changes in the XSLT and in SharePoint Designer if you really want to customize the display of your Search Results.

This is how my presentation of the Search Results look like: 
searchresultsthree15

I love feedback and comments :)

If you liked this article please leave a comment with feedback so I know that people read it, otherwise it's boring to write these step by step illustrated articles.

Cheers, and good weekend.

 
Posted on 28-Oct-07 by Zimmergren
30 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags: MCTS, XSLT, SharePoint Designer, Search Results Page, Search
 

Links to this post (Trackbacks/Pingbacks)

Comments

Thursday, 24 Apr 2008 09:58 by
nice article

Wednesday, 30 Apr 2008 06:44 by Liam
Thanks for that! Sure beats editing the XSL by hand :)

Monday, 2 Jun 2008 12:49 by Dhanyatha
hi, I have customised my XSLT displaying People search results (as mentioned above), but now i would like to display error messages when a query returns no result(just like the default results page) Can any one please suggest a way for doing so? Thanks in advance....

Monday, 2 Jun 2008 12:53 by Dhanyatha
hi, I have customised my XSLT displaying People search results (as mentioned above), but now i would like to display error messages when a query returns no result(just like the default results page) Can any one please suggest a way for doing so? Thanks in advance....

Wednesday, 2 Jul 2008 06:22 by Aniket
Hi I have created some metadata properties for searchig. How do I add this metadata/column in search result. Lets say example I have added fname and lname two metadata properties, how do I added in XSLT, so my customized search result should display in table format. Ashish

Friday, 4 Jul 2008 09:52 by Student
bra kurs du kör, kul med anpassad sök.

Sunday, 13 Jul 2008 07:56 by Ani
Hello I figure out a solution to retreive list item id. First you need to add a new Search Managed Property to the search configuration. Please do the following steps 1. Go into the SSP, Search Settings, Managed Properties, click on "New Managed Property" 2. Give it a like "ItemID" 3. choose type of Integer 4. In the "Mappings to crawled properties" section, choose "Include values from a single crawled property based on the order specified" 5. click on Add Mapping button, search for the one you want (which is ows_ID) like this in the selection dialog, select it and click OK: Now add this column in query editor column of search core result web part, and then customize xslt mentioned by zimmer. So once you create XML , you will notice that your itemid will appear in your search result xml, thne do th need dfule as per zimmer article thne you can set 'Title' field as Hyper link with your list item, so your hyper link will be "../dispform.aspx?id={itemid}. This url can be vary depending on your document or form library. I have implemented this solution successfully Cheers!!!

Thursday, 7 Aug 2008 11:22 by Frank Hellwig
Hi Tobias, You did a great job on the article! I have a question concerning sorting and Paging. I like to sort the results. Therefore I used the basic Table layout in SP-Designer and marked the table headers to be enabled for sorting and filtering in the data >view properties menu< like you mentioned it in your article. Unfortunately a click on the headers doesn’t take effect in the web part. If I save the aspx-Site in SP-Designer and run it in my SP the sorting will work fine. Do you have any ideas what to do to get that running inside core search result web part? As I know one round trip will only return max 50 Items. The sorting should sort on all potentially found items. Looking forward to hearing from you or any interested person at this blog. Thanks a lot Frank

Thursday, 23 Oct 2008 07:54 by Bander Alsharfi
Good article =)

Tuesday, 20 Jan 2009 01:09 by Dave
Hi Tobias Great article BUT you cannot add Sorting, Filtering and Grouping as it needs to repost the search query back to the server again to render the filter etc....unless you know of a way this can be done?

Tuesday, 20 Jan 2009 12:22 by Dave
Hi Tobias Great article BUT you cannot add Sorting, Filtering and Grouping as it needs to repost the search query back to the server again to render the filter etc....unless you know of a way this can be done?

Tuesday, 24 Feb 2009 11:39 by Rajmahesh
Hi, My search is matching and listing all the site names also. Is it possible to display only pages and hide the sites. Thanks

Monday, 16 Mar 2009 09:13 by Sunil
Hi. Thanks a lot for your posts. My job has now become easier. I have a question. My requirement is to implement translation of the labels in the page (like 'Results by relevance'). Is it possible by modifying the XSLT. Please suggest if there is any other way to implement this. Thanks in advance, Sunil

Sunday, 19 Apr 2009 08:28 by Yaron
Thank You Very Much for this post. It helps me a lot

Tuesday, 5 May 2009 12:48 by Chris Shepherd
Hi Tobias, Great posts - thanks for that just what I needed to know. Just thought you should know that the links to part 1 & 2 are broken because thy have .aspx.aspx on the end. Thanks Chris

Wednesday, 6 May 2009 09:22 by Chris Shepherd
Hi Tobias, Great posts - thanks for that just what I needed to know. Just thought you should know that the links to part 1 & 2 are broken because thy have .aspx.aspx on the end. Thanks Chris

Wednesday, 13 May 2009 03:47 by Peter
Hi Tobias, I have the same question as Dhanyatha. How do I display error messages when a query returns no results? Thanks

Thursday, 28 May 2009 11:18 by nilu
simply loved it.....

Tuesday, 2 Jun 2009 04:31 by that's great
Hi Tobias, do you have any ideas on how to customize the Search Statistics WebPart? Thanks

Monday, 20 Jul 2009 02:26 by Jonas
Thanks for a great article!

Tuesday, 21 Jul 2009 05:31 by Ali
Great article Tobias. Nice job. I loved the Note about not being able to see the full web part after I had already struggled for 15 minutes to figure out how to do it :)

Monday, 17 Aug 2009 02:24 by Gk
As per your article,I deleted the search core results web part from web parts maintenance page. Please tell me how and from where to add it again to the search results page. Thanks in advance.

Monday, 17 Aug 2009 02:25 by Gk
As per your article,I deleted the search core results web part from web parts maintenance page. Please tell me how and from where to add it again to the search results page. Thanks in advance.

Monday, 17 Aug 2009 02:29 by Gk
As per your article,I deleted the search core results web part from web parts maintenance page. Please tell me how and from where to add it again to the search results page. Thanks in advance.

Friday, 28 Aug 2009 08:14 by Andrés
Hi, it's a very good article!! Please you know any other resources with good samples about XSLT + Sharepoint Designer? Bye thanks

Saturday, 19 Sep 2009 11:49 by Ram
Hi, Search is not working in my sharepoint site. when i enter any text, document name, user name it shows the error that web page can not find. What might be the cause? Any help please.

Tuesday, 20 Oct 2009 01:31 by Mary
Hi, thanks for this great post. I am trying to add a message "No search results were found" to the results page when no results were found. I am guessing that the logic should be similar to the one implemented in the Search Summary web part by changing the message "Did you mean" by the one specified above. How can I accomplish this? Thanks in advance.

Tuesday, 17 Nov 2009 10:19 by Sa
Hi, I have the same problem as Frank. I can't get the sorting working. The columns are showing the descending/ascending options but nothing happens. How do I get this working? Thanks in advance

Wednesday, 9 Dec 2009 05:19 by Daniel Keegan
What an amazingly valuable walkthrough, this knowledge will prove immensely useful in our organisation! Thanks so much!

Name:
URL:
Email:
Comments: