# CRUD operations with roles

To manage roles, use the `Telerik.Sitefinity.Security.RoleManager` class. You can get an instance of the manager with the default roles data provider. However, this will not work with the out-of-the-box roles, such as *Administrators*, *Editors*, *Authors*, because they are coming from the AppRoles provider. So you must get an instance of the provider by passing the `SecurityManager.ApplicationRolesProviderName` as a parameter:

```C#
using Telerik.Sitefinity.Security;

namespace SitefinityWebApp
{
   public class ForDevelopersRoles
   {
       public void WorkWithDefaultRoles()
       {
           //work with roles that come from the default role provider

           RoleManager roleManager = RoleManager.GetManager();

           //custom logic goes here

           roleManager.SaveChanges();

       }
       public void WorkWithOtherProvider()
       {
           //work with roles that come from another provider

           RoleManager roleManager = RoleManager.GetManager(SecurityConstants.ApplicationRolesProviderName);
           
           //custom logic goes here

           roleManager.SaveChanges();
       }
   }
}
```

For more information, see [For developers: Roles data providers](https://www.progress.com/documentation/sitefinity-cms/for-developers-roles-data-providers).

## Create roles

The following example creates a role.\
First, you get an instance of the roles manager. Then, you create the role by calling the `CreateRole`method of the manager. Finally, you save the changes.

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Roles.ManagingRoles
{
   public partial class RolesApiSnippets
   {
       public void CreateRole(string roleName)
       {
           RoleManager roleManager = RoleManager.GetManager();

           if (roleManager.GetRoles().Where(r => r.Name == roleName).FirstOrDefault() == null)
           {
               roleManager.CreateRole(roleName);
           }
           else
           {
               // Handle appropriately when the role already exists
           }

           roleManager.SaveChanges();
       }
   }
}

```

## Query roles

You can query a role by:

* ID
* Name

**Querying a role by ID**

The following example queries for a role by its `ID`.\
First, you get an instance of the roles manager. Then, you get all roles by calling the `GetRoles` method of the manager and filter based on the `ID`property.

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Roles.ManagingRoles
{
   public partial class RolesApiSnippets
   {
       public Role GetRole(Guid roleId)
       {
           RoleManager roleManager = RoleManager.GetManager();

           return roleManager.GetRoles().Where(r => r.Id == roleId).SingleOrDefault();
       }
   }
}

```

**Querying a role by name**

The following example queries for a role by its name.\
First, you get an instance of the roles manager. Then, you get all roles by calling the `GetRoles` method of the manager and filter based on the `Name`property.

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Roles.ManagingRoles
{
   public partial class RolesApiSnippets
   {
       public Role GetRoleByName(string roleName)
       {
           RoleManager roleManager = RoleManager.GetManager();

           return roleManager.GetRoles().Where(r => r.Name == roleName).SingleOrDefault();
       }
   }
}

```

**Querying all roles**

The following example queries for all roles\
First, you get an instance of the roles manager. Then, you get all roles by calling the `GetRoles` method of the manager.

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Roles.ManagingRoles
{
    public partial class RolesApiSnippets
    {
        public List<Role> GetAllRoles()
        {
            RoleManager roleManager = RoleManager.GetManager();

            return roleManager.GetRoles().ToList();
        }
    }
}

```

## Modify roles

The following example modifies a role by changing its name.\
To modify a role, you get an instance of the role. In this example, you get in instance of the role by its `ID`. For more information, see *Query roles* above. Then, you change the name and, finally, you save the changes.

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Roles.ManagingRoles
{
   public partial class RolesApiSnippets
   {
       public void ModifyRole(Guid roleId, string newRoleName)
       {
           RoleManager roleManager = RoleManager.GetManager();

           Role role = roleManager.GetRoles().Where(r => r.Id == roleId).FirstOrDefault();
           if (role != null)
           {
               role.Name = newRoleName;
               roleManager.SaveChanges();
           }
       }
   }
}

```

## Delete roles

The following example deletes a role.\
To delete a role, you get an instance of the role. In this example, you get an instance of the role by its `ID`. For more information, see Query roles above. Then, you delete the role by calling the `Delete`method of the manager and passing the role as an argument. Finally, you save the changes.

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Roles.ManagingRoles
{
   public partial class RolesApiSnippets
   {
       public void DeleteRole(Guid roleId)
       {
           RoleManager roleManager = RoleManager.GetManager();

           Role role = roleManager.GetRoles().Where(r => r.Id == roleId).FirstOrDefault();
           if (role != null)
           {
               roleManager.Delete(role);
               roleManager.SaveChanges();
           }
       }
   }
}

```
