Skip to content

Instantly share code, notes, and snippets.

@ech01
Last active September 13, 2021 18:41
Show Gist options
  • Save ech01/0b9bdb33a5656c2fe3882975fe5c2da9 to your computer and use it in GitHub Desktop.
Save ech01/0b9bdb33a5656c2fe3882975fe5c2da9 to your computer and use it in GitHub Desktop.
DNN Scheduler Example
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using DotNetNuke.Services.Mail;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Scheduling;
namespace MyPortal
{
public class MyPortalUserExpiration : SchedulerClient
{
public MyPortalUserExpiration(ScheduleHistoryItem objScheduleHistoryItem)
{
ScheduleHistoryItem = objScheduleHistoryItem;
}
private void CheckUserRoles()
{
DateTime today = DateTime.Today;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["SiteSqlServer"]))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM [UserRoles] INNER JOIN [Users] ON [UserRoles].[UserID] = [Users].[UserID] WHERE [UserRoles].[ExpiryDate] = @expDate AND [Users].IsDeleted = 0", conn);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.SelectCommand.Parameters.Add("@expDate", today.AddDays(30).ToShortDateString());
da.Fill(ds, "users");
try
{
if (ds.Tables["users"].Rows.Count > 0)
{
string users = "Expiring Users: ";
foreach (DataRow row in ds.Tables["users"].Rows)
{
string userid = DotNetNuke.Entities.Users.UserController.GetUserById(0, Convert.ToInt16(row["UserID"].ToString())).ToString();
string email = DotNetNuke.Entities.Users.UserController.GetUserById(0, Convert.ToInt16(row["UserID"].ToString())).Email.ToString();
string name = DotNetNuke.Entities.Users.UserController.GetUserById(0, Convert.ToInt16(row["UserID"].ToString())).FirstName.ToString() + " " + DotNetNuke.Entities.Users.UserController.GetUserById(0, Convert.ToInt16(row["UserID"].ToString())).LastName.ToString();
string expireDate = row["ExpiryDate"].ToString();
users += row["UserID"].ToString() + " ";
string body = "Dear " + name + ",<br><br>";
body += "Your website account will expire on " + expireDate + ". If your account expires, you no longer will have access to secure materials on our website.<br><br>Please take a few minutes to review your account information and renew your access using the following link:<br><br>";
body += "<a href='http://www.MyPortalonline.org/MyMyPortal/Profile' target='_blank'>MyPortal Profile</a><br><br>";
Mail.SendEmail("[email protected]", email, "Your MyPortal Website Account Is About to Expire, Renew Now", body);
}
ScheduleHistoryItem.AddLogNote(users);
}
else
{
ScheduleHistoryItem.AddLogNote("No Users Found.");
}
ScheduleHistoryItem.AddLogNote(" Task Complete.");
}
catch (Exception e)
{
ScheduleHistoryItem.AddLogNote(" Task Failed: " + e.ToString());
Exceptions.LogException(e);
}
}
}
public override void DoWork()
{
try
{
Progressing();
CheckUserRoles();
ScheduleHistoryItem.Succeeded = true;
ScheduleHistoryItem.AddLogNote("MyPortal_User_Expiration completed.");
}
catch (Exception exc) //REQUIRED
{
ScheduleHistoryItem.Succeeded = false;
ScheduleHistoryItem.AddLogNote("MyPortal_User_Expiration failed." + exc);
Errored(ref exc);
Exceptions.LogException(exc);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment