次の方法で共有


ListViewByQuery Class

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

Inheritance Hierarchy

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)

Syntax

<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class ListViewByQuery _
    Inherits WebControl

Dim instance As ListViewByQuery
[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

Examples

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.

Imports System
Imports System.Runtime.InteropServices
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Serialization
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.SharePoint.WebPartPages

Namespace MyNamespace
   Public Class myWebPart
      Inherits System.Web.UI.WebControls.WebParts.WebPart
      
      Public Overrides Sub RenderControl(writer As HtmlTextWriter)
         Dim view As New ListViewByQuery()
         Dim currentweb As SPWeb = SPContext.Current.Web
         Dim list As SPList = currentweb.Lists("Documents")
         view.List = list
         Dim query As 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)
      End Sub 'RenderControl
   End Class 'myWebPart
End Namespace 'MyNamespace
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.

Dim webSite As SPWeb = SPContext.Current.Site.RootWeb
myListQuery.List = webSite.Lists("Announcements")

Dim query As 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
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" />

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

ListViewByQuery Members

Microsoft.SharePoint.WebControls Namespace