Random Thoughts of a Scatterbrain.
 Saturday, January 13, 2007

Adding Users To A Document Workspace

1/13/2007 6:49:08 PM (Eastern Standard Time, UTC-05:00)

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
RSS 2.0 Atom 1.0 CDF