Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help getting value of a custom field

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
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:
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top