This documentation is archived and is not being maintained.

SPQuery.ViewXml Property

Windows SharePoint Services 3

Gets or sets the XML schema that defines the view.

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

Public Property ViewXml As String
Dim instance As SPQuery
Dim value As String

value = instance.ViewXml

instance.ViewXml = value

Property Value

Type: System.String
A string that contains the view schema in Collaborative Application Markup Language.

The ViewXml property contains a string that corresponds to the inner XML of the View element in CAML.

The following code example uses the ViewXml property to define a query in CAML.

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.AllWebs("Site_Name")
    Dim list As SPList = webSite.Lists("List_Name")

    Dim query As New SPQuery()
    query.ViewXml = "<View><Query><OrderBy><FieldRef Name='ID'/>" + _
        "</OrderBy><Where><Or><Geq><FieldRef Name='Field1'/>" + _
        "<Value Type='Number'>1500</Value></Geq><Leq>" + _
        "<FieldRef Name='Field2'/><Value Type='Number'>500</Value>" + _
        "</Leq></Or></Where></Query><ViewFields>" + _
        "<FieldRef Name='Title'/>" + _
        "<FieldRef Name='Field1'/><FieldRef Name='Field2'/>" + _
        "<FieldRef Name='Field3'/><FieldRef Name='Field4'/>" + _
    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
End Try

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.