public void addUserToGroup( string groupName, string userLoginName) { using (SPSite site = new SPSite(strUrl)) { using (SPWeb web = site.OpenWeb()) { try { web.AllowUnsafeUpdates = true ; SPUser spUser = web.EnsureUser[userLoginName]; if (spUser != null ) { SPGroup spGroup = web.Groups[groupName]; if (spGroup != null ) spGroup.AddUser(spUser); } } catch (Exception ex) { throw ex; } finally { web.AllowUnsafeUpdates = false ; } } } } |
Here you should provide the login name in correct format (DomainName\UserName). For example you can call this method like follow,
addUserToGroup("Test Group", "SA\\codeitsimple");
No comments:
Post a Comment