Get username from SharePoint User field in List

本小妞迷上赌 提交于 2020-01-22 10:05:02

问题


I have a custom sharepoint workflow that I'm developing in Visual Studio. The workflow is running against a document library which has a custom content type connected to it. The content type includes a user lookup field ("owner").

I'm trying to have my workflow assign a task to the "owner" lookup field. However, I've only been able to get the display name for the user, not the account username.

Can anyone help?


回答1:


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;



回答2:


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.



来源:https://stackoverflow.com/questions/1835355/get-username-from-sharepoint-user-field-in-list

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