I have a DataTable having Some rows. Is there any way to execute CAML Query on it.
Or Alternatively, How can I convert this DataTable into a SPList so tha CAML can be executed on SPList.
private DataTable Process(DataTable d, string s)
{
DataTable dd = d;
SPQuery query = new SPQuery();
query.Query = s;
query.ViewAttributes = "Scope = 'RecursiveAll'";
DataTable dtSearch = new DataTable();
SPList Dest = new SPList();
dtSearch = d.GetItems(query).GetDataTable();
return dtSearch;
}
In above Code d.GetItems can not be done. ???
Solution tried:
I tried this from technet.
private SPList dtConvertSPList(DataTable dt)
{
SPList newList = null;
using (SPSite site = new SPSite("http://sharepointvm"))
{
using (SPWeb web = site.OpenWeb())
{
Guid newListGuid = web.Lists.Add("TempList", "This is my temp list, it will be removed when used", SPListTemplateType.GenericList);
newList = web.Lists[newListGuid];
if (newList != null)
{
/* Loop through the datatable and add columns to the list */
foreach (DataColumn dc in dt.Columns)
{
newList.Fields.Add(dc.ColumnName, SPFieldType.Text, false);
}
/* Update the list to get the new fields */
newList.Update();
/* Populate the list from the datatable */
SPListItem newItem = null;
foreach (DataRow row in dt.Rows)
{
/* Create a new item in the list */
newItem = newList.Items.Add();
foreach (DataColumn dc in dt.Columns)
{
newItem[dc.ColumnName] = row[dc].ToString();
newItem.Update();
}
}
}
}
}
return newList;
}
But the problem being faced by me is: the outer for loop i.e.
foreach (DataRow row in dt.Rows)
{
/* Create a new item in the list */
newItem = newList.Items.Add();
foreach (DataColumn dc in dt.Columns)
{
newItem[dc.ColumnName] = row[dc].ToString();
newItem.Update();
}
}
this outer loop is executing only once out of surprise.
*Debug Findings: The DataTable passed to this function had 3358 records. newList is created Fine with all columns but only one row that too with incomplete information in its columns.
Improvement Needed:
The loop should work fine and where should i put the code to delete this newList after returnig it?