SharePoint中的"用户或用户组"栏

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();
         }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值