<summary>
Get SPUser from a user type field,in SharePoint, it obvious a SPUser field, but u can ' t get it directly
</summary>
<param name= "state" ></param>
<param name= "TaskId" ></param>
public static SPUser Getworkflowuser (SPListItem taskItem, string key) {
We have no way to get the SPUser type directly from a field name in SharePoint. So we need another class, that's spfielduservalue/spfielduservaluecollection.
1. First get the value of key by Spfielduser
Spfielduser SPField = Taskitem.fields[key] as Spfielduser;
if (SPField! = null) {
If we get the value of that item, we have to judge the type of the value first and get it first.
var spfielduser = SPField. GetFieldValue (Taskitem[key]. ToString ());
Determine the type of this object, whether it is a collection or a non-collection
if (Spfielduser is spfielduservaluecollection) {
Get a specific type of object
Spfielduservaluecollection Spuservalue = SPField. GetFieldValue (Taskitem[key]. ToString ()) as spfielduservaluecollection;
if (spuservalue! = null) {
Return spuservalue[0]. User;
}
return null;
}
if (Spfielduser is Spfielduservalue) {
Spfielduservalue Uservalue = SPField. GetFieldValue (Taskitem[key]. ToString ()) as Spfielduservalue;
if (uservalue! = null) {
return uservalue.user;
}
return null;
}
}
return null;
}
SharePoint obviously added a user to this line, but couldn't get it directly with SPUser?