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”);

Advertisements

3 Responses to SharePoint : User Management – WSS

  1. Geeta says:

    Hi,

    i got the idea that programatically hw to add the user to the site.

    I have one problem with the user role. The user having limited access tries to check out the document ,then the running workflow should cancel.
    how to handle this, hw u help in this?
    how to handle through event handler?

    hw u people help me out in this?

    With Regards,
    Geeta

  2. vamsankar says:

    Hi,

    You set the role permissions, for example, the “Reader” Role users only allow the read the site contents. In this time the Reader Role permissions set “View” only in the “Site Permissions” page.

    Steps: Go to, Site Actions > Site Settings > Advanced permissions, here you add the user role and give the permissions what you want.

    thanks

  3. Himadrish says:

    Excellent!

    Thanks for the WSS information. After searching a hectic way, lastly I found something that works.

    My best wishes for your upcomming post 🙂

    Cheers!
    Himadrish

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: