Friday, 23 August 2013

Add all authenticated users to sharepoint security group programmatically

Add all authenticated users to sharepoint security group programmatically


One of the projects I came across a scenario where I need to pre populate custom security groups with “All authenticated Users”. I have wrote a code and run it as a part of the feature receiver.
If you have a security group with read access to a site collection, you can add “All authenticated users” into this groups and every user will have read access to that site.
In the feature receiver class FeatureActivated event I have used the following code.
1
2
3
4
5
6
7
8
9
SPWeb myWeb = properties.Parent as SPWeb;
permissionLevel = "Read";
myWeb.SiteGroups.Add("Site Read Group", owner, owner, "My New Category");
SPRoleAssignment roleAssignment = new SPRoleAssignment(myWeb.SiteGroups["Site Read Group"]);
roleAssignment.RoleDefinitionBindings.Add(myWeb.RoleDefinitions[permissionLevel]);
myWeb.RoleAssignments.Add(roleAssignment);
myWeb.Update();
myWeb.SiteGroups["Site Read Group "].AddUser("c:0(.s|true", string.Empty,
string.Empty, string.Empty);
Note: The "c:0(.s|true" means “All Authenticated Users”

No comments:

Post a Comment