This topic has not yet been rated - Rate this topic

ListViewByQuery Class

Windows SharePoint Services 3
Renders a list view within a Web Part or ASPX page according to a specified query.

Namespace: Microsoft.SharePoint.WebControls
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
[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"/>
System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      Microsoft.SharePoint.WebControls.ListViewByQuery
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 Additions

ADD
© 2013 Microsoft. All rights reserved.