How To Get All Permission Levels In SharePoint 2013 Programmatically

Welcome to a blog on how to get all permission levels in SharePoint 2013 programmatically using a console application. We will use Visual Studio to get all permission levels in SharePoint 2013 site.

Let’s see how to do it.
  • Open you Visual Studio.
  • Select New Project.

     
  • Select Console Application.
  • Add the references.
    Microsoft.SharePoint dll and Microsoft.SharePoint.Client dll
  • Paste the code below under Program.cs.
Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. namespace Getallpermissionlevels {  
  8.     class Program {  
  9.         static void Main(string[] args) {#call the web  
  10.             ClientContext clientContext = new ClientContext("http://devtest.us01.apmn.org/SPTests/");  
  11.             Web web = clientContext.Web;#get all permission levels  
  12.             RoleDefinitionCollection roleDef = web.RoleDefinitions;  
  13.             clientContext.Load(roleDef);#execute the code  
  14.             clientContext.ExecuteQuery();#loop through all the permission levels  
  15.             foreach(RoleDefinition roleDefinition in roleDef) {  
  16.                 Console.WriteLine(roleDefinition.Name);  
  17.             }#Display them here  
  18.             Console.ReadKey();  
  19.         }  
  20.     }  
  21. }  
  • Run the code and your document library will be created.
 
Ebook Download
View all
Learn
View all