Search This Blog

Thursday 26 July 2012

Get SPUser Object from a SharePoint list item people or group field value.

Hi All,

Refer the below code to get the SPUser object from a People of Group column. To call the function you have pass the listitem and the fieldname (display name).

public SPUser GetSPUserObject(SPListItem spListItem, String fieldName)
        {
            SPUser spUser = null;
            try
            {
                if (fieldName != string.Empty)
                {
                    SPFieldUser field = spListItem.Fields[fieldName] as SPFieldUser;
                    if (field != null && spListItem[fieldName] != null)
                    {
                        SPFieldUserValue fieldValue = field.GetFieldValue(spListItem[fieldName].ToString()) as SPFieldUserValue;
                        if (fieldValue != null)
                        {
                            spUser = fieldValue.User;
                        }
                    }
                }
           }
            catch (Exception ex)
           {
                throw ex;
            }
            return spUser;
       }

Reference :

http://www.codekeep.net/snippets/128d498e-d2ef-4901-b537-39aa12767d06.aspx

No comments:

Post a Comment