Thursday, 30 January 2014

DotNetNuke / DNN - Method to delete user role programmatically in C#.NET

This is a simple method how to delete the dnn user role programmatically in C#.NET.

Method

private void DeleteUserRoleByName(string username, string roleName)
    {
        //Get Current Dnn Portal ID
        var portalID =
            DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings().PortalId;

        //Initialize RoleController
        var rc = new DotNetNuke.Security.Roles.RoleController();

        //Retrieve role
        var ri = rc.GetRoleByName(portalID, roleName);

        // Delete User role
        rc.DeleteUserRole(
            portalID, 
            DotNetNuke.Entities.Users.UserController.GetUserByName(portalID, username).UserID, ri.RoleID );

    }

Usage

DeleteUserRoleByName("username", "rolename");

No comments: