2011-09-13 107 views
0

我有這個代碼創建SharePoint用戶組的客戶端列表。如何在SharePoint 2010的列表中選擇用戶組名稱?

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列表中的每個客戶端創建用戶本身?
如何將這些創建的組分配給列表中的用戶?

回答

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); 
     } 
    } 
} 
相關問題