C# SharePoint ListItem 错误:“值不在预期范围内”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/657241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SharePoint ListItem Error: "Value does not fall within the expected range"
提问by Ries
Hi I am developing using the SharePoint namespace and I ran into the following error when I try to retrieve a Title field from the list items.
您好,我正在使用 SharePoint 命名空间进行开发,当我尝试从列表项中检索标题字段时遇到以下错误。
Value does not fall within the expected range
值不在预期范围内
I know however that the field exists because I printed out all the fields.
但是,我知道该字段存在,因为我打印了所有字段。
string value = (string)listItem[listItem.Fields["Title"].Id];
Console.WriteLine("Title = " + value);
Update: To what extent does the View that was used to retrieve the list items play a role in what fields will be available? This code fails with the same exception:
更新:用于检索列表项的视图在多大程度上在哪些字段可用?此代码失败并出现相同的异常:
SPListItemCollection items = list.GetItems(list.DefaultView);
foreach (SPListItem listItem in items)
{
try
{
Console.WriteLine("Title = " + listItem.Title);
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
In both cases the list.DefaultView property was used to retrieve the list items.
在这两种情况下,list.DefaultView 属性都用于检索列表项。
回答by vitule
The "Title" field may exist in the list but not in the default view. Can you do this?
“标题”字段可能存在于列表中,但不存在于默认视图中。你能做这个吗?
foreach (var item in list.Items) Console.WriteLine((string)item["Title"]);
回答by Andrey Stukalin
I don't know why, but I created a GridView from SPListItemCollection as a DataSource. I had the same error. But I tried to get the dataTable from collection:
我不知道为什么,但我从 SPListItemCollection 创建了一个 GridView 作为数据源。我有同样的错误。但我试图从集合中获取数据表:
SPListItemCollection res = da.getAllItems();
DataTable dt = res.GetDataTable();
And then I see in my GridView that field that contains titles of list items is called "LinkTitle". So, I rewrited my code: SPListItemCollection res = da.getAllItems();
然后我在 GridView 中看到包含列表项标题的字段称为“LinkTitle”。所以,我重写了我的代码: SPListItemCollection res = da.getAllItems();
gridView.DataSource = res.Cast<SPListItem>().Select(a=> a["LinkTitle"] );
gridView.DataBind();
And everything works fine :)
一切正常:)
回答by Greg Sansom
The "Title" field is not (by default) available in the default view. If you have a look at the view settings page, Title is checked, but it is labeled as being "linked to item with edit menu".
“标题”字段在默认视图中不可用(默认情况下)。如果您查看视图设置页面,则会选中“标题”,但将其标记为“链接到带有编辑菜单的项目”。
The field exposed by the view is actually named "LinkTitle" (you can confirm this by enumerating list.DefaultView.ViewFields
).
视图公开的字段实际上被命名为“LinkTitle”(您可以通过枚举来确认这一点list.DefaultView.ViewFields
)。
The solution to your problem is simply to replace item.Title
with item["LinkTitle"].ToString()
.
解决您的问题的方法很简单,只需将其替换item.Title
为item["LinkTitle"].ToString()
.
回答by Perry33
I'd suggest something like
我建议像
if (item.Fields.ContainsField("Last_x0020_Modified"))
{
if (query.ViewFields.Contains("Last_x0020_Modified"))
...
B/c if the field wasn't requested in the SPQuery.ViewFields, you probably can't get it (exceptions include Created field, ID field)
B/c 如果 SPQuery.ViewFields 中未请求该字段,则您可能无法获取它(例外包括 Created 字段、ID 字段)
回答by Rahul Gokani
I don't know if your error is solved or not. But I was facing the same issue and I found the solution to the problem.
不知道你的错误解决了没有。但是我遇到了同样的问题,我找到了问题的解决方案。
You have to go to:
Central Administration > Application Management > Manage Web Applications
select the Web Application in use and choose:
“General Settings > Resource Throttling” via the Ribbon.
您必须转到:
管理中心 > 应用程序管理 > 管理 Web 应用程序
选择正在使用的 Web 应用程序,然后
通过功能区选择:“常规设置 > 资源限制”。
In that scroll down and look for "List View Lookup Threshold
" in that by default value is 8
increase the value till the error is gone.
在该向下滚动并查找“ List View Lookup Threshold
”中,默认值是8
增加值直到错误消失。
回答by Ernest Correale
I had this issue because the column was not part of the default view. Instead of relying on the view, query for the specific columns directly when creating the SPListItemCollection.
我遇到了这个问题,因为该列不是默认视图的一部分。创建 SPListItemCollection 时,不依赖于视图,而是直接查询特定列。
SPList cityList = web.Lists[cityListName];
SPListItemCollection items = cityList.GetItems("Title","LocCode");
foreach (SPListItem item in items)
{
cityName = item["Title"].ToString().Trim();
cityCode = item["LocCode"].ToString().Trim();
}