从SPFieldUser获取值,AllowMultipleValues仅在计时器作业中失败

时间:2010-11-26 14:22:34

标签: sharepoint-2010 timer-jobs spfield

这个很奇怪。

我正在SharePoint 2010中的计时器作业中执行此代码...

...
// Get the field by it's internal name
SPField field = item.Fields.GetFieldByInternalName(fieldInternalName);

if (field != null)
{
     SPFieldUser userField = (SPFieldUser)field;
     object value = null;

     if (userField.AllowMultipleValues)
     {
          // Bug when getting field value in a timer job? Throws an ArgumentException
          users = new SPFieldUserValueCollection(item.ParentList.ParentWeb, item[userField.Id].ToString());
     }
     else
     {
          // Get the value from the field, no exception
          value = item[userField.Id];
     }
}
...

此代码在简单的ConsoleApplication中运行时非常有效,但在SharePoint 2010中的计时器作业上下文中运行时,它会在行中抛出ArgumentException ...

users = new SPFieldUserValueCollection(item.ParentList.ParentWeb, item[userField.Id].ToString());

我尝试了许多变体来从SPFieldUser中检索一个值但是只有在Timer Job正在执行它并且该字段的AllowMultipleValues属性设置为TRUE时才会失败。

我尝试使用Reflector进行调试,似乎在SPListItem中抛出异常...

public object this[Guid fieldId]
{
  get
  {
    SPField fld = this.Fields[fieldId];
    if (fld == null)
    {
      throw new ArgumentException();
    }
    return this.GetValue(fld, -1, false);
}
...

这就是异常堆栈跟踪......

System.ArgumentException was caught
Message=Value does not fall within the expected range.
Source=Microsoft.SharePoint
StackTrace:
     at Microsoft.SharePoint.SPFieldMap.GetColumnNumber(String strFieldName, Boolean bThrow)
     at Microsoft.SharePoint.SPListItemCollection.GetColumnNumber(String groupName, Boolean bThrowException)
     at Microsoft.SharePoint.SPListItemCollection.GetRawValue(String fieldname, Int32 iIndex, Boolean bThrow)
     at Microsoft.SharePoint.SPListItem.GetValue(SPField fld, Int32 columnNumber, Boolean bRaw, Boolean bThrowException)
     at Microsoft.SharePoint.SPListItem.get_Item(Guid fieldId)
     at FOCAL.Point.Applications.Audits.AuditUtility.GetPeopleFromField(SPListItem item, String fieldInternalName)

叹息......有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我相信你可以增加List View Lookup Threshold的描述here。但请将其视为快速解决方案。您可能需要调整查询数据。

答案 1 :(得分:1)

这通常意味着您在单个SPQuery中请求了太多查找字段,这会导致内容数据库中的true-lookup-table的自连接过多,除非SharePoint Foundation限制了资源。对于普通用户,每个查询有一个阈值设置为8次查找。确保您的查询仅返回必要的查找或人员/组字段。如果无法减少使用量,请考虑更改阈值设置。