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("http://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("http://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("http://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("http://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("http://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("http://karthickmain:9091/default.aspx").OpenWeb();


                                    SPGroupCollection groups = mySite.SiteGroups;


                                    groups.Remove("NewGroup");


                                    MessageBox.Show("Group Removed");


 


-        //Add site group


                                    SPWeb mySite = new SPSite("http://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("http://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("http://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("http://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 (2)
  1. StevenRo says:

    Is there a way to add permissions to a site group from a string (or string array) that has all of the permissions in it?  By example, if I pulled the permissions from another site group on a different list to replicate here and had put the original permissions into a string in an element in XML.  I have a need to do this and have been banging my head against it all day.

    Steve

  2. Ahmad says:

    useful information, thanks for sharing.

Comments are closed.

Skip to main content