How to Delete a Permission Level in SharePoint Office 365 Programmatically using C# Client Side Object Model (CSOM)


Sathish Nadarajan
SharePoint MVP
Published On :   10 Apr 2017
Visit Count
Today :  1    Total :   768
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


In the earlier articles, (CREATE & ASSIGN) we saw how to create and assign the permission levels. In this article, let us see, how to delete a permission level. Usually, this will be required, when we create itself. i.e., Before Create any of the Permission Level, we should validate whether the permission level is already there or not. If it is already there, then based on our requirement, either we can delete and recreate or we can utilize the same. In our case, let us see, how to delete the permission level.

The below code has a Foreach loop to iterate all the RoleDefinitions. Actually that is not required if we know that there is a Role Definition is available. But if we are uncertain about the availability, it is always to use the Foreach.

 namespace Console.Office365
 {
     using Microsoft.SharePoint.Client;
     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
 
     class Program
     {
         static void Main(string[] args)
         {
             DeletePermissionLevel();
         }
 
         public static void DeletePermissionLevel()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://***.sharepoint.com/sites/communitysite";
             string userName = "Sathish@****.onmicrosoft.com";
             string password = "*************";
 
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.Load(web.AllProperties);
                 clientContext.Load(web.RoleDefinitions);
                 clientContext.ExecuteQueryRetry();
                 var roleDefinitions = web.RoleDefinitions;
 
                 // Delete the Custom Permission Level if Already Exists
                 foreach (var roledefinition in roleDefinitions)
                 {
                     if (roledefinition.Name == "MyPermissionLevelCreatedByCode")
                     {
                         RoleDefinition customOwnersPermissionLevel = web.RoleDefinitions.GetByName("MyPermissionLevelCreatedByCode");
                         customOwnersPermissionLevel.DeleteObject();
                         clientContext.Load(web.RoleDefinitions);
                         clientContext.ExecuteQueryRetry();
                         break;
                     }
                 }
 
                  
 
             }
         }
 
     }
 }
 

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

SharePoint Analytics