【问题标题】:SPList Item get value - ArgumentExceptionSPList 项获取值 - ArgumentException
【发布时间】:2009-10-20 17:12:54
【问题描述】:

我有一个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
}

【问题讨论】:

    标签: sharepoint splistitem argumentexception


    【解决方案1】:

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

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

    通常,当您使用农场帐户测试您的程序时,代码会运行,而对于普通用户,您会收到 ArgumentException。

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

    【讨论】:

    • 为什么我花了这么长时间才找到这个答案?为什么 SharePoint 没有给我一个更好的错误消息,例如,“嘿,您知道您要获取其值的字段吗?嗯,我在您正在访问的 SPListItem 中看到它,但它实际上并不存在,因为您的SPQuery 并没有说要检索它。继续将字段添加到您的 SPQuery 中,您将获得成功。”
    【解决方案2】:

    不仅检查 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();  
      }
    }
    

    【讨论】:

      【解决方案3】:

      自定义级联字段(或列)也有类似的情况。我按照以下方式进行操作,它似乎适用于自定义字段类型。

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

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

          例如:

          SPList checklist = //Some initiliaztion
          
          foreach (SPListItem item in checklist.Items){
               //work
          }
          

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

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

          也许试试演员表?

          (String)item[colname]
          

          【讨论】:

            猜你喜欢
            • 2012-02-14
            • 1970-01-01
            • 1970-01-01
            • 2019-08-11
            • 2023-03-21
            • 2021-10-31
            • 1970-01-01
            • 1970-01-01
            • 2011-02-07
            相关资源
            最近更新 更多