如何在SharePoint 2010中的列表中选择用户组名称?

时间:2011-09-13 09:20:38

标签: sharepoint sharepoint-2010

我有一些客户端列表,通过这些客户端我使用此代码创建了SharePoint用户组。

namespace CreateGroupCSharp.EventReceiver1
{
    public class EventReceiver1 : SPItemEventReceiver
    {  
       public override void ItemAdded(SPItemEventProperties properties)
       {
           using (SPSite site = new SPSite("http://abc/"))
           {          
               SPWeb web = site.AllWebs[0];

               SPList customList = web.Lists["Client"];

               string strCount = properties.ListItem.Title.ToString();

               string status = properties.Status.ToString();            

               SPGroup groupOwner = web.SiteGroups.GetByID(int.Parse(web.Properties["vti_associateownergroup"]));

               string groupName = strCount;

               web.SiteGroups.Add(groupName, groupOwner, null, "Custom SharePoint Group for Demo");

               SPGroup wcmGroup = web.SiteGroups[groupName];

               SPRoleDefinition designerRoleDefinition = web.RoleDefinitions["Contribute"];

               SPRoleAssignment roleAssignment = new SPRoleAssignment(wcmGroup);

               roleAssignment.RoleDefinitionBindings.Add(designerRoleDefinition);

               web.RoleAssignments.Add(roleAssignment);
               wcmGroup.Update();                              
               web.Update();              
           }

           base.ItemAdded(properties);
       }
    }
}

是否可以为SharePoint列表本身中的每个客户端创建用户?
如何将这些创建的组分配给列表中的用户?

1 个答案:

答案 0 :(得分:0)

我会将名为Users的Person或Group列添加到列表中。允许多个选择并仅允许选择人(无组)。然后,您可以使用事件接收器中的以下代码将用户添加到组中:

private void AddUsers(SPWeb web, SPGroup group, SPListItem item)
{
    object value = item["Users"];
    if (null != value)
    {
        SPFieldUserValueCollection userValues = 
            new SPFieldUserValueCollection(web, value.ToString());
        foreach (SPFieldUserValue userValue in userValues)
        {
            SPUser user = userValue.User;
            if (null == user)
            {
                user = web.EnsureUser(userValue.LookupValue);
            }
            group.AddUser(user.LoginName, user.Email, user.Name, null);
        }
    }
}