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.

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”

More Blogs

Contact us

To begin your digital transformation, get in touch.

We’re pleased to address any inquiries you might have and assist you in selecting the service that best suits your requirements.

Your benefits:
Speak To Us