Home > development, Sharepoint > Using a ListViewWebPart for Search Results

Using a ListViewWebPart for Search Results

I have had to try and create a Search page that displays its result set in the style of a ListViewWebPart.  This has proven quite tricky.

After a lot of Searching I found various articles on using SPQuery and then the List.RenderAsHtml(Query) to achieve this.  However I found a rather large problem with this method, whilst it queries what I want it to the resulting HTML looks nice but does not do any kind of paging also whilst the sort and filter  drop downs appear at the top of the columns, they don’t actually do any sorting or filtering.

Now that was completely unacceptable.

After a lot more searching to not much avail, I stumbled across a single line of code that told me that I could render the view as Html, not just the list.

I did some testing in a Custom aspx page to just call

SPList list = SPContext.Current.Web.Lists["MyList"];

string s=list.DefaultView.RenderAsHtml(false, false, “Lists/MyList/Summary.aspx”);  //*

*Note that Summary is my custom view.

This worked perfectly, it displayed what I wanted and paged and filtered and sorted.

On its own this doesn’t meet all my requirements I need a complex Query to be executed based on certain Form parameters.  So the next step was to try altering the Query.

Well the View has a Query value on it so lets try that

//This line does not work
list.DefaultView.Query = <Where><Eq><FieldRef Name=’ID’ /><Value Type=’Counter’>1</Value></Eq></Where>”;

the Query property is read-only. So the next thing I tried was lets just create a new one based on the old perhaps that will allow changes.  Well it does and this bit o code will work.

SPList list = SPContext.Current.Web.Lists["MyList"];

XmlDocument domDoc = new XmlDocument();
domDoc.LoadXml(list.DefaultView.SchemaXml);
SPView view = new SPView(list, domDoc);

view.Query = <Where><Eq><FieldRef Name=’ID’ /><Value Type=’Counter’>1</Value></Eq></Where>”;

myLabel.Text=view.RenderAsHtml(false, false, “Lists/MyList/Summary.aspx”);  // Where MyLabel is an asp Label in my Page

Now that’s done with the only hard bit is creating your CAML query on the fly based on your form values, which let’s face it isn’t that hard at all.

 

A simple Feature could be made from this, it could contain

1 custom page in _layouts (or module copied)
It has a drop down asking you to select the list you want  (created by iterating through the lists on the site).
It might also contain a dropdown to select the view you want
It contains a Large TextBox area for typing in a CAML query
A Button to “Go”

It Contains a Label to spit out the results.

This is dead simple but might be a really handy feature for messing about with CAML, you could use it to test the Query for your custom view.

 

 

  1. M Thakkar
    May 15th, 2009 at 22:56 | #1

    Hello,

    I need to do same thing for doc library. Any siggestions?

    regards,

    MT

  2. Binaryjam
    May 17th, 2009 at 13:29 | #2

    I haven’t had to do that so Im not sure, havent the time to check for you either sorry.

  3. Satheesh
    May 19th, 2009 at 21:20 | #3

    Hi,

    I have a very similar requirement, but i should not go for custom aspx (or atleast it should not be stored in layouts folder), instead can i extend the listview webpart or any alternative solution.

    regards
    Satheesh

  4. Binaryjam
    May 27th, 2009 at 16:32 | #4

    Not sure about extending the list view web part but a custom webpart doing this kind of thing should be easy t knock up.

    In a web part you would be able to select any list and also the view you want , you could go mad and dynamically create the search params to query, go mad.

  1. No trackbacks yet.