SPQuery - 更新列表项需要哪些字段 (ViewFields)?
-
10-12-2019 - |
题
在 SPQuery 中,更新列表项需要哪些字段?
SPQuery query = new SPQuery();
query.Query = @"
<OrderBy>
<FieldRef Name='ID' Ascending='true' />
</OrderBy>";
query.ViewFields = string.Concat(
"<FieldRef Name='ID' />",
"<FieldRef Name='Title' />");
query.ViewFieldsOnly = true;
query.RowLimit = 2000; // Effective batch size.
query.QueryThrottleMode = SPQueryThrottleOption.Override;
如果我跑 listItem.Update()
在不更改任何字段的情况下,更新失败并出现错误 'value does not fall within the expected range'
. 。但如果我改变 ViewFieldsOnly
为 false 则更新成功。我的问题是需要哪些字段才能成功更新列表项(不包括任何可能已更改的字段)?
解决方案
AFAIK 它没有记录在任何地方应该包含哪些字段以便能够在将 ViewFieldOnly 设置为 true 的查询后进行更新,但根据 SPQuery.ViewFieldsOnly 和更新 SPListItem 它包括其他 ID、UniqueID、对象类型……
但 我的建议是绝对不要尝试,只需将 ViewFieldsOnly 设置为 false,那么即使在 SharePoint 的未来版本中,您也能确保获得所需的内容。
其他提示
我也遇到过这个问题,并且已经设法解决了。我将 ListItem ID 包含在 ViewFields 中,然后使用 List.GetItemByID 创建了一个新的 SPListItem 变量。然后,您可以使用这个新变量进行正常的列表项更新。
SPQuery q = new SPQuery();
q.Query = "Query here";
q.ViewFields = "...Fields Here...<FieldRef Name='ID' />";
q.ViewFieldsOnly = true;
foreach (SPListItem item in list.GetItems(q))
{
SPListItem i = list.GetItemById(int.Parse(item["ID"].ToString()));
//code to update item here
i.Update();
}
它可能不是最好的解决方案,但它让我开始运行。