0

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..

2 Answers 2

4

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

4
  • 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
    Commented May 31, 2012 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? Commented May 31, 2012 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
    Commented May 31, 2012 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 Commented May 31, 2012 at 10:34
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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