我正在尝试使用以下方法在SharePoint中查询多个任务列表:
SPSiteDataQuery query = new SPSiteDataQuery();
query.Lists = "<Lists ServerTemplate=\"171\" />";
query.Query = string.Concat("<Where>",
"<Neq>",
"<FieldRef Name='Checkmark' />",
"<Value Type='Calculated'>Completed</Value>",
"</Neq>",
"</Where>");
query.Webs = "<Webs Scope=\"Recursive\" />";
query.ViewFields = ViewFields = "<FieldRef Name='Title' /><FieldRef Name='AssignedTo' />";
SPListItemCollection dtTasks = web.GetListItem(query);
return dtTasks;
如果AssignedTo字段设置为UserMulti(https://support.microsoft.com/en-us/kb/2703054),则不起作用。
我以为我可以通过循环列表(按标题硬编码到代码中)来解决这个问题,然后收集列表项结果:
string[] listCollection = { "List1", "List2" };
SPListItemCollection collection = null;
foreach (var list in listCollection)
{
SPList spList = web.Lists.TryGetList(list);
if (spList != null)
{
SPQuery qry = new SPQuery();
qry.Query =
@"<Where>
<And>
<Leq>
<FieldRef Name='DueDate' />
<Value Type='DateTime'>
<Today />
</Value>
</Leq>
<Neq>
<FieldRef Name='Checkmark' />
<Value Type='Calculated'>Completed</Value>
</Neq>
</And>
</Where>";
qry.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='AssignedTo' />";
collection = spList.GetItems(qry);
}
}
return collection;
但这当然会覆盖每个列表的SPListItemCollection集合。
如何为每次迭代存储列表中的项目?
答案 0 :(得分:1)
您可以使用DataTable
从SPListItemCollection
获取collection.GetDataTable()
,然后使用DataTable.Merge()
方法将结果合并到一个数据表中。
foreach (var list in listCollection)
{
var results = new DataTable();
SPList spList = web.Lists.TryGetList(list);
if (spList != null)
{
SPQuery qry = new SPQuery();
qry.Query = "...";
results.Merge(spList.GetItems(qry).GetDataTable());
}
}
return results;
或者,将您的查询中的SPListItem
添加到您在foreach之外宣布的新空SPListItemCollection
,如下所示。这样,您就可以直接访问这些项目。
SPListItemCollection result = new SPListItemCollection();
foreach (var list in listCollection)
{
...
foreach (SPListItem listItem in spList.GetItems(qry)){
result.Add(listItem);
}
result.Update();
}
return result;