Get username from a SharePoint user field in the list

I have a custom sharepoint workflow that I am developing in Visual Studio. The workflow runs against the document library to which the custom content type is connected. The content type includes a user search field ("owner").

I am trying to have my workflow assign a task in the owner search field. However, I was only able to get the display name for the user, not the account username.

Can anyone help?

+3
source share
2 answers

, .

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; 
 }

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

:

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;
}

:

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

.

+4

Source: https://habr.com/ru/post/1724541/


All Articles