Get username from SharePoint User field in List

谁都会走 提交于 2019-12-03 03:59:33
Kusek

Refer to this Article on how to get the User Details from the Field.

public static SPUser GetSPUser(SPListItem item, string key) {
     SPFieldUser field = item.Fields[key] as SPFieldUser;

     if( field != null) {   
         SPFieldUserValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldUserValue; 

         if(fieldValue != null)     
            return fieldValue.User; 
      }
      return null; 
 }

Your Code should be like this

SPUser spUser=GetSPUser(splistItem,"Owner");
String sUserName=(spUser!=null)?spUser.UserName:null;
Rohan Mhatre

My solution:

public static SPUser GetSPUser(SPListItem item, string key)   
{
    SPUser user=null;   
    SPFieldUserValue userValue = new SPFieldUserValue(item.Web, item[key].ToString());
    if (userValue != null)
    {
        SPUser user = userValue.User;
    }
return user;
}

How To Call:

SPUser spUser=GetSPUser(splistItem,"Owner"); 

This is tested code and working fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!