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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<span style="font-family: Courier;"><span style="color: #0000ff;">using</span><span style="color: #000000;">(SPSite site = </span><span style="color: #0000ff;">new </span><span style="color: #000000;">SPSite(</span><span style="color: #ff00ff;">"http://ashelia:2345"</span><span style="color: #000000;">)) {</span> <span style="color: #0000ff;">using</span><span style="color: #000000;">(SPWeb workspace = site.OpenWeb()) {</span> <span style="color: #0000ff;">string </span><span style="color: #000000;">resourceLogin = </span><span style="color: #ff00ff;">"ASHELIA\\cchen"</span><span style="color: #000000;">;</span></span> <span style="font-family: Courier;"> <span style="color: #008000;">// Ensure that the user exists and conveniently, get</span> <span style="color: #008000;">// an SPUser reference.</span> <span style="color: #000000;">SPUser user = workspace.EnsureUser(resourceLogin);</span></span> <span style="font-family: Courier;"> <span style="color: #008000;">// Create a new SPRoleAssignment for the user.</span> <span style="color: #000000;">SPRoleAssignment assignment =</span> <span style="color: #0000ff;">new </span><span style="color: #000000;">SPRoleAssignment(</span> <span style="color: #000000;">user.LoginName, user.LoginName,</span> <span style="color: #000000;">user.Name, user.Notes</span> <span style="color: #000000;">);</span></span> <span style="font-family: Courier;"> <span style="color: #008000;">// Add the "Contribute" role definition to the role</span> <span style="color: #008000;">// assignment.</span> <span style="color: #000000;">assignment.RoleDefinitionBindings.Add(</span> <span style="color: #000000;">workspace.RoleDefinitions[</span><span style="color: #ff00ff;">"Contribute"</span><span style="color: #000000;">]</span> <span style="color: #000000;">);</span></span> <span style="font-family: Courier;"> <span style="color: #008000;">// Add the assignment to the web.</span> <span style="color: #000000;">workspace.RoleAssignments.Add(assignment);</span></span> <span style="font-family: Courier;"> <span style="color: #008000;">// Update the web.</span> <span style="color: #000000;">workspace.Update();</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span></span> |
Note that when you create a new sub-web, by default, there are 5 role definitions defined for you already. These are:
- Full Control
- Design
- Contribute
- Read
- Limited Access