SharePoint obviously added a user to this line, but couldn't get it directly with SPUser?

Source: Internet
Author: User


<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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.