Sharepoint:PeopleEdit 使用

 

 

人员选择器PeopleEditor如何使用

ArrayList list = PeopleEditor1.ResolvedEntities ;
获取id和显示名
foreach (Microsoft.SharePoint.WebControls.PickerEntity p in list)
{
string userId = p.EntityData["SPUserID"].ToString();
string DisplayName = p.EntityData["DisplayName"].ToString();              
}          

//获取帐号
ArrayList selectedAccoutList = PeopleEditor1.Accounts;
string selectedAccouts2 = PeopleEditor1.CommaSeparatedAccounts;
//设置值
PeopleEditor1.CommaSeparatedAccounts = @"JYSERVER\spsadmin,JYSERVER\administrator";
/// <summary>
/// 从PeopleEditor取值
/// </summary>
/// <param name="peopleEditor"></param>
/// <returns></returns>
        public static List<SPPrincipal> GetSPPrincipalsFromPeopleEditor(PeopleEditor peopleEditor)
{

List
<SPPrincipal> rtn = new List<SPPrincipal>();
SPWeb web
= SPContext.Current.Web;
SPUserCollection users
= web.SiteUsers;
SPGroupCollection groups
= web.SiteGroups;

foreach (PickerEntity pe in peopleEditor.ResolvedEntities)
{
   string principalType = pe.EntityData["PrincipalType"].ToString();
   if (principalType == "User" || principalType == "SecurityGroup")
   {
      string loginName=pe.Key;
      foreach (SPUser u in users)
      {
          if (u.LoginName == loginName)
          {
            rtn.Add(u);
            break;
           }
      }

    }
   else if (principalType == "SharePointGroup")
     {
       string groupName = pe.Key;
       foreach (SPGroup g in groups)
      {
       if (g.Name == groupName)
         {
         rtn.Add(g);
           break;
          }
        }
     }
}
return rtn;
}

/// <summary>
/// 为PeopleEditor赋值
/// </summary>
/// <param name="peopleEditor"></param>
/// <param name="spPrincipals"></param>
        public static void SetPeopleEditorValue(PeopleEditor peopleEditor, List<SPPrincipal> spPrincipals)
{
string principalsStr;
if (spPrincipals==null || spPrincipals.Count == 0)
principalsStr
= "";
else
{
List
<string> principalsStrCollection = new List<string>();
foreach (SPPrincipal principal in spPrincipals)
{
SPUser user
= principal as SPUser;
SPGroup group
= principal as SPGroup;
if (user != null || group != null)
principalsStrCollection.Add(principal.Name);
}
principalsStr
= string.Join(",", principalsStrCollection.ToArray());
}
peopleEditor.CommaSeparatedAccounts
= principalsStr;

=============================================================================================

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值