This documentation is archived and is not being maintained.

SPQuery.ViewFields Property

Windows SharePoint Services 3

Gets or sets the fields that are returned in the query.

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

'Declaration
Public Property ViewFields As String
	Get
	Set
'Usage
Dim instance As SPQuery
Dim value As String

value = instance.ViewFields

instance.ViewFields = value

Property Value

Type: System.String
A string that contains a fragment in Collaborative Application Markup Language that specifies the view fields.

The ViewFields property allows you to specify which fields to return in a query on a list.

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

The following code example uses the ViewFields property to specify two fields to return in a 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.AllWebs("Site_Name")
Try
     Dim list As SPList = webSite.Lists("List_Name")

    Dim query As New SPQuery()
    query.ViewFields = "<FieldRef Name='Field1'/>" + _
        "<FieldRef Name='Field2'/>"
    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 
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.

Show: