SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using SPQuery to fetch data (particular columns from the list based on viewID) from SharePoint list. Here is the code for fetching the data:

SPQuery sQuery = new SPQuery();
currentlistview = splist.GetView(new Guid(listViewID));
foreach (string viewField in currentlistview.ViewFields)
  SPField spfield = splist.Fields.GetField(viewField);
  sQuery.ViewFields += "<FieldRef Name='" + viewField + "' />";
sQuery.ViewAttributes = "Scope=\"Recursive\"";
sQuery.ViewFieldsOnly = true;
DataTable dt =  splist.GetItems(sQuery).GetDataTable()

But I am getting all the columns from the SharePoint list. Could you provide a solution for this issue ?

share|improve this question
Are you looking to get the same results from a view into your datatable? You can send the GetItems the SPView object to do so. – David Lozzi Nov 30 '11 at 14:58
Did you check whether currentlistview.ViewFields is actually returning all the fields in the list? – Kit Menke Nov 30 '11 at 15:50
kit Menke : I have checked the currentlistview.ViewFields returns only the particular columns(not all the columns) in the list. So what am I doing wrong ? Could you help me to solve this issue ? – Ramalingam Dec 1 '11 at 5:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.