Saturday, July 17, 2010

The ListViewQuery webpart

This web part displays data using the ListViewQuery object model component. The ListViewByQuery control is part of the Microsoft.SharePoint and is located in the Microsoft.SharePoint.WebControls namespace.
an example in C#:

ListViewByQuery _view = new ListViewByQuery();
 _view.List = "List Title";
SPQuery _query = new SPQuery(_view.List.Views["ListView"]);
 _view.Query = _query;
this.Controls.Add(_view);

The idea is that you can create a list and its views at any level of your site collection and then use a very simple and intuitive custom webpart to display these views through the all site collection.

Below is a scenario about how to use the webpart:
  • You have a list/library at the root web of your site collection in which you store information about a series of countries called Country Profiles by Regions. 
  • You have a sub-site below your root web called Western Europe Region and you want to display data stored in the Country Profiles by Regions library previously created on the root web.
So this is what you do:

You go back to the root web of your site collection and create a view in the Country Profiles by Region list called Western Europe.

You filter the view to only display countries belonging to the Western Europe Region.
You go back to the Western Europe Region sub-site and on the home page you drop the ListViewQuery webpart and display the newly created view.

Click here to download the solution
(After you download the folders extract all the files from the SimpleWebParts_1.0.zip to your SharePoint 2007 Server and follow the instructions from the ListViewQuery-Installation-Guide.docx.