Showing posts with label SPListItem. Show all posts
Showing posts with label SPListItem. Show all posts

Wednesday, November 11, 2009

How to get SharePoint user information from Person or Group field in SharePoint List

Scenario: I have a SharePoint list with various fields. One of them is Person field, in which I am storing the value selected through People Picker. How to get the Person value from this field through SharePoint Object Model.

Approach:


// Step1: Get SharePoint Item object
SPListItem Item = SPContext.Current.Web.Lists["MyList"].Item[0];

// Step2: Get user field object by passing field display name
SPFieldUser userField = (SPFieldUser)Item.Fields.GetField("Created By");

// Step3: Get user field value object
// SPBuiltInFieldId.Author refers to Created By field's internal name
SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(Item[SPBuiltInFieldId.Author].ToString());

// Step4: Get user object
SPUser userVendor = fieldValue.User;