Tuesday, December 2, 2014

Adding “Everyone” Permissions Programmatically in SharePoint 2013

Introduction

Normally, we are having permission level operations in SharePoint. We may assign such kind of permission both manually and programmatically. Here, we will see how to give permission to "Everyone" programmatically

Code


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

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

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

list.Update();

Powershell script

Set-SPUser -Identity "c:o(.s\true" -web "webUrl" -Group "Visitors"

2 comments:

  1. It is not working, but when I give "Everyone" instead of "c:o(.s\true" it is working and new-spuser command then only its wokring

    new-spuser -UserAlias "Everyone" -Web "http://testsitecollection" -Group "Approvers"

    ReplyDelete