标签:
/// <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) {
//SharePoint中我们没有办法通过一个field名称直接拿到SPUser类型。所以需要借助另一个class,那就是SPFieldUserValue/SPFieldUserValueCollection.
//1.先通过SPFieldUser拿到key的一项的值
SPFieldUser spfield = taskItem.Fields[key] as SPFieldUser;
if (spfield != null) {
//如果我们拿到那一项中是有值的,那就得先判断一下那一项值的类型,先拿到再说.
var spfieldUser = spfield.GetFieldValue(taskItem[key].ToString());
//判断这对象的类型,是集合还是非集合
if (spfieldUser is SPFieldUserValueCollection) {
//拿到有具体类型的对象
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明明这一行添加了一个User,但是却不能直接用SPUser拿到它?
标签:
原文地址:http://www.cnblogs.com/Q-lucia/p/4335558.html