Get username from SharePoint User field in List

前端 未结 2 1168
轻奢々
轻奢々 2021-02-06 18:35

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. Th

相关标签:
2条回答
  • 2021-02-06 18:50

    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;
    
    0 讨论(0)
  • 2021-02-06 19:10

    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.

    0 讨论(0)
提交回复
热议问题