Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SPQuery Class

Windows SharePoint Services 3

Represents a query in a list view.

System.Object
  Microsoft.SharePoint.SPQuery

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

'Declaration
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public Class SPQuery
'Usage
Dim instance As SPQuery

The following code example displays the titles of items in a Tasks list where the Status column equals Completed. The example uses Collaborative Application Markup Language (CAML) to define the query.

This example requires using directives (Imports in Microsoft Visual Basic) for the [Microsoft.SharePoint] and [Microsoft.SharePoint.Utilities] namespaces.

Dim webSite As SPWeb = SPContext.Current.Site.RootWeb
Try
    Dim list As SPList = webSite.Lists("Tasks")

    Dim query As New SPQuery()
    query.Query = "<Where><Eq><FieldRef Name='Status'/>" + _
      "<Value Type='Text'>Completed</Value></Eq></Where>"
    Dim items As SPListItemCollection = list.GetItems(query)

    Dim item As SPListItem
    For Each item In  items
        Response.Write((SPEncode.HtmlEncode(item("Title").ToString()) + _
          "<BR>"))
    Next item
Finally
    webSite.Dispose()
End Try
NoteNote

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

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

Community Additions

ADD
Show:
© 2015 Microsoft