sharepoint中使用PeopleEditor控件为列表项赋值示例

来源:互联网 发布:迟夙生的网络办公室 编辑:程序博客网 时间:2024/05/16 11:02
 

sharepoint中使用PeopleEditor控件示例

 

protected void Page_Load(object sender, EventArgs e){     try    {         SPWeb web = SPContext.Current.Web;         // get a handle to the list we’ll be pulling values from        SPList list = web.Lists["Demo"];         // using the querystring parameter containing the id, get the list item we’ll be dealing with        SPListItem listItem = list.GetItemById(Convert.ToInt32(Request["id"]));         // the managers column is of type Person or Group, so we can use a spfielduservaluecollection to     store the values from it        SPFieldUserValueCollection users = (SPFieldUserValueCollection)listItem["Managers"];         // our array will hold the entities we’ll use to eventually assign to the people editor control        ArrayList entityArrayList = new ArrayList();         // loop through each use in the collection, set the key, add to the array        for (int i = 0; i < users.Count; i++)        {             PickerEntity entity = new PickerEntity();            entity.Key = users[i].User.LoginName;            entityArrayList.Add(entity);         }         Managers.UpdateEntities(entityArrayList);     }     catch (Exception ex)    {         Response.Write(ex.ToString());     } }


-----------------------------------

 

protected void btnSubmit_Click(object sender, EventArgs e){    // create a web object with context for the current site     SPWeb web = SPContext.Current.Web;     // get the entries that were entered into the people editor and store them in a string     string managers = peManagers.CommaSeparatedAccounts;     // commaseparatedaccounts returns entries that are comma separated. we want to split those up     char[] splitter = { ',' };     string[] splitPPData = managers.Split(splitter);     // this collection will store the user values from the people editor which we'll eventually use    // to populate the field in the list     SPFieldUserValueCollection values = new SPFieldUserValueCollection();     // for each item in our array, create a new sp user object given the loginname and add to our collection     for (int i = 0; i < splitPPData.Length; i++)    {        string loginName = splitPPData[i];         if (!string.IsNullOrEmpty(loginName))        {            SPUser user = web.SiteUsers[loginName];             // you could also use SPUser user = web.EnsureUser(loginName);             SPFieldUserValue fuv = new SPFieldUserValue(web, user.ID, user.LoginName);             values.Add(fuv);        }     }     // set the Person or Group column     SPListItemCollection listItems = web.Lists["Demo"].Items;     SPListItem manager = listItems.Add();     manager["Managers"] = values;     manager.Update();}

http://blog.sina.com.cn/s/blog_53864cba0100jjhn.html

原创粉丝点击