Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
SPQuery Class
SPQuery Properties
 ViewFields Property

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (0)
SPQuery.ViewFields Property (Microsoft.SharePoint)
Gets or sets the fields that are returned in the query.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
Public Property ViewFields As String
Visual Basic (Usage)
Dim instance As SPQuery
Dim value As String

value = instance.ViewFields

instance.ViewFields = value
C#
public string ViewFields { get; set; }

Property Value

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 ( Default Namespace )Microsoft.SharePoint and ( Default Namespace )Microsoft.SharePoint.Utilities namespaces.

Visual Basic
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 
C#
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Site_Name"])
{
    SPList oList = oWebsite.Lists["List_Name"];

    SPQuery oQuery = new SPQuery();
    oQuery.ViewFields = "<FieldRef Name='Field1'/>" +
        "<FieldRef Name='Field2'/>";
    SPListItemCollection colListItemsAvailable = 
      oList.GetItems(oQuery);

    foreach (SPListItem oListItemAvailable in colListItemsAvailable)
    {
        Response.Write(SPEncode.HtmlEncode(oListItemAvailable.Xml) + 
          "<BR>");
    }
}
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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
ViewFields and ViewXml?      brianpmccullough   |   Edit   |   Show History
What happens when you use ViewFields and ViewXml together? For example, I define both on my query, and in ViewXml I define a ViewFields element. Do the values in ViewFields takes precedence over those in the ViewXml?
Tags What's this?: Add a tag
Flag as ContentBug
Handy piece of code      LZandman ... pau   |   Edit   |   Show History

I've created the following piece of code that can aid you when specifying ViewFields:

public static string BuildViewFieldsXml(params string[] fieldNames)
{
const string TEMPLATE = @"<FieldRef Name='{0:S}'/>";
StringBuilder sb = new StringBuilder();
foreach (string fieldName in fieldNames)
{
sb.AppendFormat(TEMPLATE, fieldName);
}
return sb.ToString();
}

Use it like this:

SPQuery query = new SPQuery();
query.ViewFields = BuildViewFieldsXml("Title", "Created", "SomeCustomField");
Returning empty fields      René Hézser   |   Edit   |   Show History
If you want your query to return empty columns, you have to add Nullable='TRUE' to the viewfields.

<FieldRef Name='Field1' Nullable='TRUE'/>

If you do not add the Nullable attribute, accessing the results of the query like this:

oListItemAvailable["Field1"] will give an Exception.

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker