Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to take a DataTable containing ID and other columns which are not hidden.

I am doing this for a generic list which contains any number/type of columns. so I don't know the field names which are in the list.

This is the code I am trying to do..

SPWeb myWeb = SPContext.Current.Web;
                    SPList myList = myWeb.Lists[ListName];
                    if (myList != null)
                    { 
                        SPQuery query = new SPQuery();
                        query.Query = "<Where><Or><Eq><FieldRef Name='Hidden' /><Value Type='Bool'>FALSE</Value></Eq><Eq><FieldRef Name='Title' /><Value Type='Text'>ID</Value></Eq><Or></Where>";
                        DataTable dtListSource = myList.GetItems(query).GetDataTable();

I am getting this error now..

One or more field types are not installed properly. Go to the list settings page to delete these fields.

I am working on SharePoint 2007(WSS3) with .NET Framework 2.0, so Linq does not seems available.

Can you tell me what I was doing wrong here? I'm pretty new to SharePoint and I hope I could caml query to SPListItem's properties like this..

share|improve this question
add comment

2 Answers 2

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

share|improve this answer
    
Hi, Thank you for your reply. I realized I can not query to filed properties other than the InternalName using FieldRef. But listField.Hidden returns true for these fields which are not usually displays in List, so with your code, the result is same as that gets with myList.Items.GetDataTable(); ID, ContentType, Modified, Created, Author, Editor, _UIVersionString, Attachments, LinkTitleNoMenu, LinkTitle, DocIcon. These are the fields I need to avoid. –  Sen Jacob May 31 '12 at 6:38
    
@BeediKumaraN - SPField.Hidden Property defines fields that CAN BE displayed in list view. My guess is that you have some predefined view you need to mimic (?) Also by looking at your answer I realized that you are buildnig webpart so I wonder what is your main goal? –  Vedran Rasol May 31 '12 at 9:14
    
Thank you for the update. I saw the property Field.CanToggleHidden. Actually I'm trying to learn SharePoint by myself for the past 3 weeks. Currently I'm experimenting with creating webparts, so I tried to display a list contents in Gridview. Now I need to learn more about ToolParts :) Could you please suggest some good tutorials in SharePoint for programmers? especially about webparts, toolparts or reporting? –  Sen Jacob May 31 '12 at 10:23
    
All clear now. I was a bit confused because you are developing something that is already there OOTB. I really don't recall any usefull tutorial on webparts in 2007 (except Hello World examples). Too bad you are on 2007 because there is nice tutorial for 2010 webparts on MSDN –  Vedran Rasol May 31 '12 at 10:34
add comment
up vote 0 down vote accepted

I've to add some check boxes for each field in myList.Fields via a ToolPart (CustomPropertyToolPart) in my WebPart and let the user select whichever columns he wants to have in the DataTable.

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.