When using CAML to extract from a list, what is the purpose of the viewfields in the query - to set value types - text, interger,etc. - and make the fields available so the get_item('fieldname') method can pull the data from them? If the viewfields is omitted can you use the get_item('fieldname') method?
<ViewFields> is used in the CAML query to define that how many fields you want to retrieve by executing the query.
If we did not define
<ViewFields> then it will retrieve all the columns of list.
For example if I define ViewFields like:
<ViewFields> <FieldRef Name="ID"></FieldRef> <FieldRef Name="Title"></FieldRef> </ViewFields>
then it will retreive only two columns ID & Titile from the list.
It's not mandatory to set
Read more: SPQuery.ViewFields Property
ViewFieldsbasically gives you option to reduce data in response.
- It allows you to specify which fields you want in return for your query.
It is always a best practice to use
ViewFields as and when possible.
SPQuery query = new SPQuery(); query.Query = <<YOUR QUERY GOES HERE>> query.ViewFields = "<ViewFields>"+ "<FieldRef Name="ID"></FieldRef>"+ "<FieldRef Name="Title"></FieldRef>"+ "</ViewFields>"; query.ViewFieldsOnly = true;