Cretae class named "RoleScheduler" and add the below code to that class.
After building the module, add this scheduler class in dotnetnuke scheduler by going to Host -> Schedule -> Add Item to Schedule
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Services.Scheduling;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Common.Utilities;
using System.Data;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security.Roles;
using DotNetNuke;
namespace YourCompany.Modules.ModuleName
{
public class RoleScheduler : SchedulerClient
{
public RoleScheduler(ScheduleHistoryItem oItem) : base()
{
this.ScheduleHistoryItem = oItem;
}
public override void DoWork()
{
try
{
//Perform required items for logging
this.Progressing();
// Adds and deletes user role
AddDeleteUserRole();
this.ScheduleHistoryItem.Succeeded = true;
this.ScheduleHistoryItem.AddLogNote("User role has been added and deleted successfully ");
}
catch (Exception exc)
{
this.ScheduleHistoryItem.Succeeded = false;
this.ScheduleHistoryItem.AddLogNote("Operation failed: " + exc.Message);
this.Errored(ref exc);
}
}
}
///
/// Adds and deletes the user role
///
public void AddDeleteUserRole()
{
DateTime dtmExpDate = DateTime.Now;
PortalSettings objPortalSettings = new PortalSettings();
RoleInfo objRoleInfo = new RoleInfo();
RoleController objRoleController = new RoleController();
objRoleController.DeleteUserRole(objPortalSettings.PortalId,
objPortalSettings.UserId,
objRoleInfo.RoleID);
objRoleController.AddUserRole(objPortalSettings.PortalId,
objPortalSettings.UserId,
objRoleInfo.RoleID, dtmExpDate);
}
}
}
Hope this will help you.
Thanks,
Avinash Kumar [ DNN Developer, Mindfire Solutions,India ]