I need help getting the value of a custom field in a list. The field name is Archived By (or Archived_x0020_By), it's a "Person or Group" field type. When I try to access it via curItem.Fields["Archived By"];...I get this error: Object reference not set to an instance of an object. at GPSNotifications.Program.getSPUser(SPListItem item, SPField userField)
When I try to access it via curItem.Fields["Archived_x0020_By"]; ...I get this error: Value does not fall within the expected range. at Microsoft.SharePoint.SPFieldCollection.GetFieldByDisplayName(String strDisplayName, Boolean bThrowException) at Microsoft.SharePoint.SPFieldCollection.get_Item(String strDisplayName)
I have a method I call to get values for this type of field:
The standard Modified By field...works fine:
My custom field that throws the error:
I have confirmed that I do have the correct Field name and internal name. Perhaps I can't access this the same way since this is a custom field?
Any help would be greatly appreciated.
When I try to access it via curItem.Fields["Archived_x0020_By"]; ...I get this error: Value does not fall within the expected range. at Microsoft.SharePoint.SPFieldCollection.GetFieldByDisplayName(String strDisplayName, Boolean bThrowException) at Microsoft.SharePoint.SPFieldCollection.get_Item(String strDisplayName)
I have a method I call to get values for this type of field:
Code:
static SPUser getSPUser(SPListItem item, SPField userField)
{
string currentValue = item[userField.Title].ToString();
SPFieldUser field = (SPFieldUser)userField;
SPFieldUserValue fieldValue = (SPFieldUserValue)field.GetFieldValue(currentValue);
return fieldValue.User;
}
The standard Modified By field...works fine:
Code:
SPField editorFld = curItem.Fields["Modified By"];
SPUser editorName = getSPUser(curItem, editorFld);
mySqlCommand.Parameters.Add(new SqlParameter("@Editor", SqlDbType.NVarChar, 50)).Value = editorName.ToString();
My custom field that throws the error:
Code:
SPField archFld = curItem.Fields["Archived_x0020_By"];
SPUser archName = getSPUser(curItem, archFld);
mySqlCommand.Parameters.Add(new SqlParameter("@ArchivedBy", SqlDbType.NVarChar, 50)).Value = archName.ToString();
I have confirmed that I do have the correct Field name and internal name. Perhaps I can't access this the same way since this is a custom field?
Any help would be greatly appreciated.