获得<SharePoint:PeopleEditor> 的值

来源:互联网 发布:win10安装ubuntu失败 编辑:程序博客网 时间:2024/06/04 17:48
 

有时候页面是要放一个PeopleEditor,然后赋值给item。当然最开始的时候想法应该是超级简单,直接把控件的value赋值给item就完事了。结果根本就不是那么一回事。

网上有人提供了方法,写了一大堆,很复杂,两眼没看懂,又感觉怎么不像自己要的。没办法,还是要自己弄一下。

不过还是要承认借鉴了网上通用的方法,只是在SharePoint2010有点变化。

页面加控件:

<SharePoint:PeopleEditor ID="people" runat="server" selectionset="User" MultiSelect="false" />

只选一个人的PeopleEditor控件

下面的方法是拿控件里的值。和网上sharepoint2007版本的有点不同,在string DisplayName行

public static string GetPeopleEditorValue(PeopleEditor objPeopleEditor)
        {
            string strResult = string.Empty;
            ArrayList list = objPeopleEditor.ResolvedEntities;
            
            foreach (Microsoft.SharePoint.WebControls.PickerEntity p in list)
            {
                string userId = p.EntityData["SPUserID"].ToString();
                string DisplayName = p.DisplayText.ToString();
                strResult += userId + ";#" + DisplayName;
                strResult += ",";
            }
            return strResult;
        }

赋值:

SPContext.Current.ListItem["人"] = GetPeopleEditorValue(people);

如果PeopleEdit里面没有选人,则是空值,ListItem["人"]就是空值,不会报什么错误的。



原创粉丝点击