Share via


SPQuery.Query Property

Gets or sets the inner XML used in the query.

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

Syntax

Public Property Query As String
    Get
    Set

Dim instance As SPQuery
Dim value As String

value = instance.Query

instance.Query = value
public string Query { get; set; }

Property Value

Type: System.String
A string that contains a fragment in Collaborative Application Markup Language that defines the query. The string corresponds to the inner XML of the Query element in CAML and must not contain the opening and closing <Query></Query> tags.

Examples

The following code example uses the Query property to define a query that returns items whose Field2 values are greater than 1000.

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("List_Name")

    Dim query As New SPQuery()
    query.ViewFields = "<FieldRef Name='Field1'/>" + _
        "<FieldRef Name='Field2'/>"
    query.Query = "<Where><Geq><FieldRef Name='Field2'/>" + _
        "<Value Type='Number'>1000</Value></Geq></Where>"
    Dim items As SPListItemCollection = list.GetItems(query)

    Dim item As SPListItem
    For Each item In  items
        Response.Write((SPEncode.HtmlEncode(item.Xml) + "<BR>"))
    Next item
Finally
    webSite.Dispose()
End Try
using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{
    SPList oList = oWebsiteRoot.Lists["List_Name"];

    SPQuery oQuery = new SPQuery();
    oQuery.ViewFields = "<FieldRef Name='Field1'/>" + 
        "<FieldRef Name='Field2'/>";
    oQuery.Query = "<Where><Geq><FieldRef Name='Field2'/>" +
        "<Value Type='Number'>1000</Value></Geq></Where>";
    SPListItemCollection collListItems = oList.GetItems(oQuery);

    foreach (SPListItem oListItem in collListItems)
    {
        Response.Write(SPEncode.HtmlEncode(oListItem.Xml) + "<BR>");
    }
}

Note

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.

See Also

Reference

SPQuery Class

SPQuery Members

Microsoft.SharePoint Namespace