ListViewByQuery Class
Renders a list view within a Web Part or ASPX page according to a specified query.
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
Microsoft.SharePoint.WebControls.ListViewByQuery
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
The following example defines the rendering of a Web Part in which the ListViewByQuery class is used to specify a query for cases where the Documents list contains "Sample" in the Subject column.
using System; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; namespace MyNamespace { public class myWebPart: System.Web.UI.WebControls.WebParts.WebPart { public override void RenderControl(HtmlTextWriter writer) { ListViewByQuery view = new ListViewByQuery(); SPWeb currentweb = SPContext.Current.Web; SPList list = currentweb.Lists["Documents"]; view.List = list; SPQuery query = new SPQuery(view.List.DefaultView); query.ViewFields = "<FieldRef Name='Title'/>"; query.Query = "<Where><Contains><FieldRef Name='Subject'/><Value Type='Text'>Sample</Value></Contains></Where>"; view.Query = query; EnsureChildControls(); view.RenderControl(writer); RenderChildren(writer); } } }
If the preceding example throws a NullReferenceException, verify that you have correctly used the List and Query properties. If you do not define the ViewFields property for the query, the example may raise an exception related to a missing list reference. If you do not create the SPQuery object with a view object, as seen in the example, the control will be rendered as a blank page.
The following example defines a query against a root Web site that returns the Title and Body column values for all items in the Announcements list where a field contains a specified user alias.
SPWeb webSite = SPContext.Current.Site.RootWeb; myListQuery.List = webSite.Lists["Announcements"]; SPQuery query = new SPQuery(myListQuery.List.Views[3]); query.ViewFields = "<FieldRef Name=\"Title\"/><FieldRef Name=\"Body\"/>"; query.Query = "<Where><Contains><FieldRef Name=\"UserField\"/><Value Type=\"Text\">john@somewhere.com</Value></Contains></Where>"; myListQuery.DisableFilter = true; myListQuery.DisableSort = true; myListQuery.Query = query;
The previous example can be applied to an ASPX page by including a page directive that registers the Microsoft.SharePoint.WebControls namespace and inserting a control like the following within the form:
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> ... <SharePoint:ListViewByQuery ID="myListQuery" runat="server"/>
Thanks,Moni
- 12/30/2010
- Moninder Singh
This is a real pity.
- 9/20/2010
- frankiestainless
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- 8/27/2010
- Rajesh.Sitaraman
- 8/31/2010
- Rajesh.Sitaraman