2007年5月8日 星期二

SharePoint 2007 -- SPListItemCollection.GetDataTable() ??????

reference from here !!

直接自己建method吧
最近怎麼常碰到這種自己必須硬來,hard coding的慘劇啊~~~~~~~~ 唉

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[0].Fields)
{

DataColumn col = new DataColumn(curField.InternalName);
tbl.Columns.Add(col);
}
foreach (SPListItem item in itemCollection)
{
DataRow row = tbl.NewRow();
foreach (SPField field in item.Fields)
{
try
{
row[field.InternalName] = item[field.InternalName];
}
catch { continue; } // Gah...
}
tbl.Rows.Add(row);
}
}
return tbl;
}

沒有留言: