Dismiss Notice

Register now to be one of the first members of this SharePoint Community! Click here it just takes seconds!

Dismiss Notice
Welcome Guest from Country Flag

How to Delete a Permission Level in SharePoint Office 365 Programmatically using C# Client...

Discussion in 'SharePoint Blogs' started by sathishnadarajan,, Apr 11, 2017.

Thread Status:
Not open for further replies.
  1. sathishnadarajan,

    sathishnadarajan, Guest

    Blog Posts:
    0
    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 = "[email protected]****.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.

    Continue reading...
     
Thread Status:
Not open for further replies.

Share This Page

LiveZilla Live Chat Software