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

Hi with following code I am retrieving a DataTable from a List:

using (SPSite site = new SPSite("http:..."))
            {
                using(SPWeb web = site.OpenWeb())
                {
                    SPList oList = web.Lists.TryGetList("listname");

                    if(oList != null)
                    {
                       DataTable dt = oList.Items.GetDataTable();
                    }
                }
            }

This works fine. My problem is that I only want to get custom ListFields. When working with Fields one can simply call FromBaseType which will show if a Field is custom or not. The only value which seems right is IsCustomType. Though it is listed under Not public members, which makes it unaccessable via code for me, right?

Well a dirty approach would be to iterate through ListFields, match them with DataColumns and check whether they are custom or not.

Is there any clean, efficient solution for this? Is this possible using CamlQueries? If yes, how would that query look like?

share|improve this question

2 Answers 2

Try this :

        using (SPSite site = new SPSite("http:..."))
        {
            using(SPWeb web = site.OpenWeb())
            {
                SPList oList = web.Lists.TryGetList("listname");

                if(oList != null)

                {
                  foreach(Field field in oList.Fields)
                  {
                   if(!field.FromBaseType)
                     {
                        //Write your code here
                     }
                    }

                }
            }
        }
share|improve this answer
1  
the answer might give the correct result but the op asked....Is there any clean, efficient solution for this? , the code is not efficient ;) , what if you have 50,000 records, than you would be looping and checking each and every single one! –  ali Sharepoint Jul 24 at 11:04

off the top of my head, I dont know but i think there is most certanly a easyier solution than what im about to propose,

        using (SPSite site = new SPSite("http:..."))
        {
            using(SPWeb web = site.OpenWeb())
            {
                    SPList list = web.Lists.TryGetList("listname");

                    SPQuery query = new SPQuery();
                    query.Query = "";
                    query.ViewFields = string.Concat(
                               "<FieldRef Name='cutsom1' />",
                               "<FieldRef Name='cutsom2' />",
                               "<FieldRef Name='cutsom3' />",
                               "<FieldRef Name='cutsom4' />");

                    query.ViewFieldsOnly = true;


                    SPListItemCollection items = list.GetItems(query);
                    if (items != null)
                    {
                        DataTable dt = items.GetDataTable();
                    }

                }


            }

method above is returning the views you set, I know what if you change them offtenly or you wont know the names of the custom columns... this you would need to loop throug each field and not item....

share|improve this answer

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.