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

Sathish Nadarajan
 
Solution Architect
April 10, 2017
 
Rate this article
 
Views
3314

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment