locked
Restricting SPQuery Fields Using ViewFields Not Working RRS feed

  • Question

  • Dear All

    I have written the following SPQuery to fetch a record from a SharePoint list and then cycle through the fields that I want. I have specified 13 fields using the .ViewFields method to restrict the number of columns returned. However, when I run through the debugger, I can see there are 73 fields being returned. 

    As far as I can see, I'm doing things correctly. I would be really grateful if anyone has any ideas:

                Using oSiteCollection As New SPSite(_ListPath)
                    Using web As SPWeb = oSiteCollection.OpenWeb()
                        List = web.GetList(_ListPath)
                    End Using
                End Using
    
                'Filter results and restrict columns returned
                Dim Query As New SPQuery
                Query.Query = "<Where><Eq><FieldRef Name='Student' /><Value Type='Text'>" & Student & "</Value></Eq></Where>"
                Query.ViewFields = "<FieldRef Name='Extra_x0020_Time' /><FieldRef Name='Reader' /><FieldRef Name='Word_x0020_Processor' /><FieldRef Name='Scribe' /><FieldRef Name='Live_x0020_Scribe' /><FieldRef Name='Live_x0020_Speaker' /><FieldRef Name='Quiet_x0020_Space' /><FieldRef Name='Prompt' /><FieldRef Name='Rest_x0020_Breaks' /><FieldRef Name='Transcripts' /><FieldRef Name='Modified_x0020_Papers' /><FieldRef Name='Educational_x0020_Psychologist' /><FieldRef Name='Teacher_x0020_Assessed' />"
                Query.ViewFieldsOnly = True
                Dim ItemColl As SPListItemCollection = List.GetItems(Query)
    
                'If any Access Arrangements have been returned
                If ItemColl.Count > 0 Then
    
                    Dim AccessArrangements As String = String.Empty
    
                    'Cycle through each item
                    For Each i As SPListItem In ItemColl
                        'Cycle through columns
                        If i.Fields.Count > 0 Then
                            For f As Integer = 0 To i.Fields.Count - 1
                               AccessArrangements += i.Fields(f).InternalName & "; "
                            Next
                        End If
    
                    Next
    
                Else
                    Return "None"
    
                End If

    Many thanks for your help

    Dan

    Tuesday, November 3, 2015 10:01 AM

Answers

All replies

  • Hi djs25uk,

    The fields showed in Visual Studio debugger are the fields collection of the SPListItemCollection, it will return the all fields the query returned.

    If specifying the SPQuery.ViewFieldsOnly to true, then if we need to get the fields which does not specify in the ViewFields property, it will throw the exception "Value does not fall within the expected range", so the ViewFields property still works for the listitem object.

    More information:

    SPListItemCollection.Fields property

    ViewFields- Value does not fall within the expected range

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Wednesday, November 4, 2015 9:08 AM
  • Thank you for your reply but I think I mislead you - I didn't meant through the debugger (I know that would show all the columns).

    In my code, I output all of the internal names of the fields to a LiteralControl. It's showing 73 different internal names when there should only be 13.

                            For f As Integer = 0 To i.Fields.Count - 1
                               AccessArrangements += i.Fields(f).InternalName & "; "
                            Next

    That's what makes me suspect that the ViewFieldsOnly is not working.

    Thanks

    Daniel

    Wednesday, November 4, 2015 11:30 AM
  • Hi djs25uk,

    The fields in the returned by the loop will contain all the fields of the item include some hidden fields, so there will be more fields than the viewfields. You can see the exact view fields in item.XML.

    It seems that you create a new thread, you can refer the Dennis's reply:

    https://social.msdn.microsoft.com/Forums/office/en-US/b5154247-c72a-4795-8f77-ae8e9c3d017e/spquery-not-restricting-fields?forum=sharepointdevelopment#892bdba7-9dad-4368-a3e4-b2280223fa74

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Friday, November 6, 2015 1:38 AM