<charliedigital /> Programming, Politics, and uhh…pineapples

13Jan/070

Adding Users To A Document Workspace

In WSS3, the process of adding users to a document workspace (or any sub-web) has changed from WSS2. The following snippet will allow you to add a user (I've only tested with users mapped to domain accounts) to the workspace:

using(SPSite site = new SPSite("http://ashelia:2345")) {
    using(SPWeb workspace = site.OpenWeb()) {
        string resourceLogin = "ASHELIA\\cchen";

        // Ensure that the user exists and conveniently, get
        // an SPUser reference.
        SPUser user = workspace.EnsureUser(resourceLogin);

        // Create a new SPRoleAssignment for the user.
        SPRoleAssignment assignment =
            new SPRoleAssignment(
                user.LoginName, user.LoginName,
                user.Name, user.Notes
            );

        // Add the "Contribute" role definition to the role
        // assignment.
        assignment.RoleDefinitionBindings.Add(
            workspace.RoleDefinitions["Contribute"]
        );

        // Add the assignment to the web.
        workspace.RoleAssignments.Add(assignment);

        // Update the web.
        workspace.Update();
    }
}

Note that when you create a new sub-web, by default, there are 5 role definitions defined for you already. These are:

  1. Full Control
  2. Design
  3. Contribute
  4. Read
  5. Limited Access
» Share
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Twitter
  • Google Bookmarks
  • LinkedIn

About Charles Chen

I'm a senior consultant with Paragon Computer Professionals. In a previous life, I was the lead developer at a startup building what became CSC's FirstPoint.
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(All comments are moderated)

No trackbacks yet.