Condividi tramite


SharePoint : User Management - WSS

Its hard to search from the SDK to find APIs and methods for the user managment. I have compiled the set of sample code snippets for the User Management in WSS

 

- //Add user to the site groups

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPRole reader = mySite.Roles["Reader"];

                                    //reader.AddUser(userName,email,displayName,notes);

                                    reader.AddUser("fareast\\pavank", "pavank@microsoft.com", "Pavan", "Reader");

                                    MessageBox.Show("User addedd");

- //Remove user from the site groups

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPUser user = mySite.AllUsers["fareast\\pavank"];

                                    SPRole reader = mySite.Roles["Reader"];

                                    reader.RemoveUser(user);

                                    MessageBox.Show("User removed");

- //Add user to the cross - site groups

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPGroup group = mySite.SiteGroups["MyGroup"];

                                    //AddUser(userName,email,displayName,notes);

                                    group.AddUser("fareast\\sumitba", "sumitba@microsoft.com", "sumitba", "Reader");

                                    MessageBox.Show("User addedd");

- //Remove user from the cross - site groups

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPGroup group = mySite.SiteGroups["MyGroup"];

                                    SPUser user = mySite.SiteUsers["fareast\\sumitba"];

                                    group.RemoveUser(user);

                                    MessageBox.Show("User Removed");

- //Add cross site group

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPGroupCollection groups = mySite.SiteGroups;

                                    SPUser user = mySite.Users["fareast\\karthisu"];

                                    SPMember member = mySite.Users["fareast\\karthisu"];

                                    groups.Add("NewGroup", member, user, "Test");

                                    MessageBox.Show("Group Added");

- //Remove cross site group

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPGroupCollection groups = mySite.SiteGroups;

                                    groups.Remove("NewGroup");

                                    MessageBox.Show("Group Removed");

- //Add site group

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPRoleCollection siteGroups = mySite.Roles;

                                    siteGroups.Add("MyGroup", "Description", SPRights.ManageWeb | SPRights.ManageSubwebs);

                                    MessageBox.Show("Role Added");

- //Remove site group

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPRoleCollection siteGroups = mySite.Roles;

                                    siteGroups.Remove("MyGroup");

                                    MessageBox.Show("Role Removed");

- //Set permission to the site group

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPRole reader = mySite.Roles["Reader"];

                                    reader.PermissionMask = SPRights.ManageLists | SPRights.ManageListPermissions;

                                    reader.Update();

                                    MessageBox.Show("Permissions updated");

- //Add group of users

                                    SPWeb mySite = new SPSite("https://karthickmain:9091/default.aspx").OpenWeb();

                                    SPUserInfo[] userInfo = new SPUserInfo[2];

                                    userInfo[0].Email = "pavank@microsoft.com";

                                    userInfo[0].LoginName = @"fareast\pavank";

                                    userInfo[0].Name = "Pavank";

                                    userInfo[0].Notes = "Test";

                                    userInfo[1].Email = "sumitbak@microsoft.com";

                                    userInfo[1].LoginName = @"fareast\Sumitba";

                                    userInfo[1].Name = "Sumitba";

                                    userInfo[1].Notes = "Test";

                                    mySite.Roles["Reader"].Users.AddCollection(userInfo);

                                    MessageBox.Show("Users Added");

Comments

  • Anonymous
    March 06, 2007
    The comment has been removed
  • Anonymous
    December 28, 2010
    useful information, thanks for sharing.