Skip to main content
2 of 2
added 4 characters in body
Vedran Rasol
  • 8.8k
  • 27
  • 52

The query in your code is searching for items where field named Hidden has value set to TRUE. My guess is that your list doesn't have field named Hidden.

I will answer your question with some code:

//Always make sure you dispose SPSite and SPWeb objects
using (SPSite site = new SPSite("http://mycoolsite"))
{
    using(SPWeb web = site.OpenWeb())
    {
        const string tmpField = "<FieldRef Name='{0}'/>";
        string viewFields = string.Empty;

        SPList myList = web.Lists["myCoolListName"];
                
        //Get all list fields 
        var listFields = myList.Fields;
        //Build View Fields
        foreach (SPField listField in listFields)
        {
            //Only add if field is not hidden
            if(!listField.Hidden)
                viewFields += string.Format(tmpField, listField.InternalName);
        }

        SPQuery query = new SPQuery();
        query.Query = ""; // You can add some condition. Items returned by query
        query.ViewFields = viewFields; //Fields returned by query

        DataTable dtListSource = myList.GetItems(query).GetDataTable();
    }
}

Some more info on MSDN: SPQuery.ViewFields

Vedran Rasol
  • 8.8k
  • 27
  • 52