SPListItem Indexer报告“值不在预期范围内”

时间:2016-01-26 23:49:30

标签: c# asp.net sharepoint

我有以下代码:

  try
  {
    string customerName = SPContext.Current.Web.CurrentUser != null ? 
        SPContext.Current.Web.CurrentUser.Name.ToString() : "Customer";

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
      try
      {
        using (SPSite site = new SPSite(this.DesignJobListUrl))
        {
          using (SPWeb web = site.OpenWeb())
          {
            web.AllowUnsafeUpdates = true;

            SPList list = web.GetList(this.DesignJobListUrl);
            SPListItem newListItem = list.Items.Add();

            newListItem["Customer Name"] = customerName;
            newListItem["Design Id"] = this.DesignStartResponse.DesignJobId;
            newListItem["Email"] = email;


            newListItem.Update();
          }
        }
      }
      catch (Exception ex)
      {
        innerEx = ex;
      }
    });

问题在于行newListItem["Customer Name"] = customerName; 抛出Value does not fall within the expected range.异常,我不明白为什么。其他两个任务工作正常。

错误发生在.NET Web应用程序的上下文中。

2 个答案:

答案 0 :(得分:1)

从这里开始:https://sharepoint.stackexchange.com/questions/50357/value-does-not-fall-within-the-expected-range-but-the-field-exist

试试这个:

newListItem.Properties["Customer Name"] = customerName;

答案 1 :(得分:0)

在设计器中确保列表字段的名称完全相同: "客户名称","设计ID","电子邮件"。 任何额外/缺失的空格,字母大小写的差异都可能导致代码无效。