Connecting to a SharePoint list I'm pulling back a ListItemCollection, which is being fed into a DataTable, however I only want to select certain fields from the ListItemCollection into the DataTable. Can anyone advise how to do this. New to C#.
Web site = cc.Web;
List targetList = site.Lists.GetByTitle("Team");
DataTable dtData = new DataTable();
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><Where><Contains><FieldRef Name='TeamClass'/><Value Type='Text'>Retail</Value></Contains></Where></Query></View>";
ListItemCollection collListItem = targetList.GetItems(query);
cc.Load(collListItem);
cc.ExecuteQuery();
for (int iCntr1 = 0; iCntr1 < collListItem.Count; iCntr1++)
{
foreach (var field in collListItem[0].FieldValues.Keys)
{
dtData.Columns.Add(field);
}
foreach (var item in collListItem)
{
DataRow dr = dtData.NewRow();
}
}
Resolved the issue by adding the <ViewFields>
clause to the xml.
CamlQuery query = new CamlQuery();
query.ViewXml =
"<View>
<Query>
<Where>
<Contains>
<FieldRef Name='Name' /><Value Type='Text'>House</Value>
</Contains></Where></Query>" +
"<ViewFields>" +
"<FieldRef Name=Ref />" +
"<FieldRef Name=Name />" +
"</ViewFields>
</View>";