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 use GetDataTable on an SPListItemCollection. I have run into a situation where my SPFieldLookupValueCollections in this list are being converted to a string when they are stored in the DataTable.

Is it possible for me to store these values as SPFieldLookupValueCollections objects instead of strings?

share|improve this question
    
Yeah I asked a similar question a while ago. TL;DR Write your own GetDataTable method (which is what you found out also it seems). –  Kit Menke Mar 26 '12 at 19:58
add comment

1 Answer

Apparently this is a known issue. I ended up using the following method that I found here, to resolve the issue:

    DataTable CreateDataTableFromListItemCollection(SPListItemCollection itemCollection)
    {
        DataTable tbl = new DataTable();
        if (itemCollection.Count > 0)
            tbl.TableName = itemCollection[0].ParentList.Title;

        if (itemCollection.Count > 0)
        {
            // Take the first item and generate all columns
            //tbl.TableName = itemCollection[0].Title;
            foreach (SPField curField in itemCollection.Fields)
            {
                DataColumn col = new DataColumn(curField.InternalName);
                tbl.Columns.Add(col);
            }
            foreach (SPListItem item in itemCollection)
            {
                DataRow row = tbl.NewRow();
                foreach (SPField field in itemCollection.Fields)
                {
                    try
                    {
                        row[field.InternalName] = item[field.InternalName];
                    }
                    catch { continue; } // Gah...
                }
                tbl.Rows.Add(row);
            }
        }
        return tbl;
    }

Hope this helps someone!

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.