Adding “Everyone” Permissions Programmatically in SharePoint 2013

Ever needed to add permissions to a list for “Everyone” in code, but nothing worked? Not NT Authenticated Users, not All Users?  Turns out, it’s super simple if you know the completely obscure account name for “Everyone”.

SPUser allUsers = web.AllUsers[@”c:0(.s|true”];
SPRoleDefinitionCollection roleCollection = web.RoleDefinitions;
SPRoleDefinition roleDefinition = roleCollection[“Contribute”];

SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)allUsers);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

list.BreakRoleInheritance(false);
list.RoleAssignments.Add(roleAssignment);

// save
list.Update();

Because why wouldn’t the account name for “Everyone” be “c:o(.s|true”?

Advertisements

2 thoughts on “Adding “Everyone” Permissions Programmatically in SharePoint 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s