2011-03-14 43 views
2

我正嘗試從自定義登錄頁面將用戶添加到現有組。現在,我從SPWeb.CurrentUser獲取當前用戶沒有任何問題。我可以查看所有這些當前用戶組,但現在我遇到了將該用戶添加到現有組的問題。我想我需要使用SPRoleDefinition和SPRoleAssignment,但我可以找到的是如何使用這些類更改組的權限。有誰知道我可以通過groupname將該用戶添加到組中嗎?SharePoint 2010:從代碼中將用戶添加到組

謝謝!

回答

3

您可以使用此功能將用戶添加到當前站點。您需要傳遞組名稱和用戶名。

public void AddUsers(string groupname, string username) 
{ 
    try 
    { 
     SPSecurity.RunWithElevatedPrivileges(delegate() 
     { 
       // Gets a new security context using SHAREPOINT\system 
       using (SPSite site = new SPSite(SPContext.Current.Site.Url)) 
       { 
        using (SPWeb thisWeb = site.OpenWeb()) 
        { 
          thisWeb.AllowUnsafeUpdates = true; 
          SPUser Name = thisWeb.EnsureUser(username); 
          thisWeb.Groups[groupname].AddUser(Name); 
          thisWeb.AllowUnsafeUpdates = false; 
        } 
       } 
    }); 

    } 

    catch (Exception ex) 
    { 
     //Log error here. 
    } 
} 
相關問題