0 out of 2 rated this helpful - Rate this topic

ListViewByQuery Class

Renders a list view within a Web Part or ASPX page according to a specified query.

System.Object
  System.Web.UI.Control
    System.Web.UI.WebControls.WebControl
      Microsoft.SharePoint.WebControls.ListViewByQuery

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class ListViewByQuery : WebControl

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"/>
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
DataSheet View Support
Hi, I have been trying to use this the ListViewByQuery control on a subsite to display a list from the parent site. It seems to support standard list views fine, however I cannot get the DataSheet view to display on the child site. Is the DataSheet View support for this.

Thanks,Moni
Unsupported External Content Types
Unfortunately it seems like the ListViewByQuery does not support Lists based on External Content Types.
This is a real pity.
Refer to SharePoint.dll latest version
As its targeting SharePoint 2010 it shouldn't be read as ?
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>