SPList 项目获取值 - ArgumentException

发布于 2024-08-08 06:38:48 字数 406 浏览 8 评论 0原文

我有一个 SPListItem 和一个列名数组。 当我尝试使用以下代码访问 SPListItem 值时:

for(int i=0;i<arrColName.length;i++)
{
    string tempValue =  item[arrColName[i]].ToString();
    // Works fine in case the the specific column in the list item is not null
    // Argument exception - Values does not fall witing expected range
    // exception in case the value //is null
}

I have an SPListItem and I have an array of column names.
When I try to access the SPListItem values using the code below:

for(int i=0;i<arrColName.length;i++)
{
    string tempValue =  item[arrColName[i]].ToString();
    // Works fine in case the the specific column in the list item is not null
    // Argument exception - Values does not fall witing expected range
    // exception in case the value //is null
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瑕疵 2024-08-15 06:38:48

我认为您使用 SPQuery 来获取列表项,但忘记将字段添加到 SPQuery 的 viewfields 属性中。

query.ViewFields = string.Format("<FieldRef Name=\"{0}\" Nullable=\"True\" />", mFieldName);

通常,当您使用农场帐户测试程序时,代码将正常工作,而对于普通用户,您会收到 ArgumentException。

导致 ArgumentException 的另一个问题/功能是新的 ListView Threshold。如果您尝试访问的列表包含太多项目,则会引发此异常。处理此问题的一种方法是使用 powershell 增加列表的阈值。

I think that you used an SPQuery to get the list items and forgot to add the field into the viewfields property of SPQuery.

query.ViewFields = string.Format("<FieldRef Name=\"{0}\" Nullable=\"True\" />", mFieldName);

Usually when you test your program with the farm account the code will work, with normal users you get an ArgumentException.

Another problem/feature which causes ArgumentException is the new ListView Threshold. If th elist you try to access has too many items, this Exception is raised. A way to handle this is to increase the threshold with powershell for the list.

违心° 2024-08-15 06:38:48

不仅检查 item != null 还检查 item["FieldName"] != null。因为如果你尝试在 null 上调用 .ToString() ,你会得到异常。

如果内部名称为“FieldName”的字段不存在,您也会收到异常。所以你可能会尝试

SPFieldCollection fields = list.Fields;
foreach (SPListItem item in list.Items) {
  if (fields.Contains("FieldName") && item["FieldName"] != null) {
    string fieldValue = item["FieldName"].ToString();  
  }
}

Not only check if item != null but also item["FieldName"] != null. Because if you will try to call .ToString() on null, you will get exception.

And if that field with internal name "FieldName" name does not exist, you will also get an exception. So you would probably try

SPFieldCollection fields = list.Fields;
foreach (SPListItem item in list.Items) {
  if (fields.Contains("FieldName") && item["FieldName"] != null) {
    string fieldValue = item["FieldName"].ToString();  
  }
}
梦幻的味道 2024-08-15 06:38:48

我在自定义级联字段(或列)中遇到了类似的情况。我按照以下方式完成了它,它似乎适用于自定义字段类型。

item.Properties["Country"] = "Mexico"; // custom field
item.Properties["nCity"] = "Cancun"; // custom field
item["Document Descriptions"] = "Test document description.";

注意:我为自定义列添加了 item.Properties。无需为内置字段类型添加属性(否则它们不起作用)。

I had a similar situation with custom cascade field (or column). I did it following way and it seemed to work for the custom field types.

item.Properties["Country"] = "Mexico"; // custom field
item.Properties["nCity"] = "Cancun"; // custom field
item["Document Descriptions"] = "Test document description.";

Note: I added item.Properties for the custom columns. No need to add properties for built in field type (else they don't work).

堇色安年 2024-08-15 06:38:48

您的数组是否包含列的内部名称或显示名称?如果是后者,您可以尝试使用 item[item.Fields[arrColName[i]].InternalName].ToStrinng(); 代替。

Does your array contain the internal names or the display names of the columns? If it's the latter you might try item[item.Fields[arrColName[i]].InternalName].ToStrinng(); instead.

亣腦蒛氧 2024-08-15 06:38:48

共享点列表不存储为具有静态大小的数组。

您必须使用内置的共享点迭代器来遍历每个元素

例如:

SPList checklist = //Some initiliaztion

foreach (SPListItem item in checklist.Items){
     //work
}

这将对 SPlist 编辑中的每个项目起作用


错误的建议,我直到编辑后才看到代码。

也许尝试一下演员阵容?

(String)item[colname]

Sharepoint Lists aren't stored as a array with a static size.

You have to use the built in sharepoint iterator to go through each element

For example:

SPList checklist = //Some initiliaztion

foreach (SPListItem item in checklist.Items){
     //work
}

This will do work on each item in your SPlist

Edit:
Wrong advice, I didn't see the code until after the edit.

Maybe try a cast?

(String)item[colname]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文