1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
//从SPListItem中取SPUser方法、代码
处理Sharepoint中的SPListItem时,有时需要获得这个对象的
"Created By"
(创建者),并且希望是SPUser类型,而不是
string
,这样可以进而得到该用户的权限、ID等等。
"Person or Group"
(用户或组)对应的类型是SPFieldUser,怎么把SPFieldUser转成SPUser呢?
网上找了些资料,发现要获得SPUser需要借助SPFieldUserValue,借鉴了一些代码,自己试了试,写了一个通用的方法:
SPUser GetSPUserFromSPListItemByFieldName(SPListItem spItem,
string
fieldName)
{
string
userName = spItem[fieldName].ToString();
SPFieldUser _user = (SPFieldUser)spItem.Fields[fieldName];
SPFieldUserValue userValue = (SPFieldUserValue)_user.GetFieldValue(userName);
return
userValue.User;
}
//SharePoint中的"用户或用户组"栏,当选择了"允许多重选择"后,用对象模型SPListItemCollection["栏名"]获得到的是SPFieldUserValueCollection的对象:
SPFieldUserValueCollection users = SPListItem[
"栏名"
]
as
SPFieldUserValueCollection;
如果将SPListItemCollection[
"栏名"
]输入字符串的话,是
"用户ID;#用户Name"
,
一 为栏赋值有这几种方式,
SPListItem[
"栏名"
] = SPUser实例或者SPGroup实例,但是不能赋SPUserCollection;
SPListItem[
"栏名"
] =
"用户ID;#用户Name;#用户ID;#用户Name......"
;
SPListItem[
"栏名"
] = SPFieldUserValueCollection实例;(当栏设置允许多重选择为否时,赋的值都是集合中的第一个)
SPListItem[
"栏名"
] = SPFieldUserValue实例;
二 获取栏的值
当栏设置为
"允许多重选择"
为否时,不管用SPFieldUserValueCollection userValues=item[
"栏名"
]
as
SPFieldUserValueCollection;还是SPFieldUserValue userValue = item[
"栏名"
]
as
SPFieldUserValue;得到的值都是Null;
如果想判断是否选择了
"允许多重选择"
,可以先查看SPFieldUser字段的AllowMutipleValues属性.不过真麻烦.
实际上在该字段在
"允许多重选择"
为否时,字段类型是
string
,为是时,字段类型是SPFieldUserValueCollection.
protected
void
btnOK_Click(
object
sender, EventArgs e)
{
ArrayList list = PeopleEditor1.ResolvedEntities;
string
msgTo =
""
;
//获取id和显示名称
foreach
(Microsoft.SharePoint.WebControls.PickerEntity p
in
list)
{
string
userId = p.EntityData[
"SPUserID"
].ToString();
msgTo += userId +
"#"
;
}
//获取帐号
ArrayList selectedAccoutList = PeopleEditor1.Accounts;
string
selectedAccouts2 = PeopleEditor1.CommaSeparatedAccounts;
SPSecurity.RunWithElevatedPrivileges(
delegate
()
{
if
(msgTo !=
""
)
{
for
(
int
i = 0; msgTo.Length > 1; i++)
{
string
lassmsgto =
string
.Empty;
lassmsgto = msgTo.Substring(msgTo.IndexOf(
"#"
) + 1);
msgTo = msgTo.Substring(0, msgTo.IndexOf(
"#"
));
SPSite site =
new
SPSite(SPContext.Current.Site.Url);
SPWeb web = site.RootWeb;
.AllowUnsafeUpdates =
true
;
web.AllowUnsafeUpdates =
true
;
SPListItem item = web.Lists[
"Messageboard"
].Items.Add();
item[
"ToNameID"
] = msgTo;
item[
"Content"
] =
this
.txtContent.Text.Trim();
item[
"Title"
] =
this
.txtTitle.Text.Trim();
SPFieldUserValue userValue =
new
SPFieldUserValue(web,msgTo);
item[
"ToName"
] = userValue;
item.Update();
web.AllowUnsafeUpdates =
false
;
site.AllowUnsafeUpdates =
false
;
msgTo = lassmsgto;
}
}
});
GridView1Show();
}
|
SharePoint中的"用户或用户组"栏
最新推荐文章于 2019-01-07 14:34:00 发布