Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://www.andrewconnell.com/Creating-Custom-SharePoint-Timer-Jobs.
- }
- public Test(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType)
- {
- this.Title = "Timer Job";
- }
- public Test(string jobName, SPWebApplication webApplication)
- : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
- {
- this.Title = "Timer Job";
- }
- private void SendEmail(string EmailBody, string Subject, string ToEmailAddress)
- {
- try
- {
- System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
- msg.To.Add(ToEmailAddress);
- msg.From = new System.Net.Mail.MailAddress("****");
- msg.Subject = Subject;
- msg.IsBodyHtml = true;
- //EmailBody = " <b>Welcome to Send an Email!!</b><p> Example.<BR>";
- msg.Body = EmailBody;
- string smtp = "*****";
- System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtp);
- System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
- NetworkCred.UserName = "**";
- NetworkCred.Password = "**";
- NetworkCred.Domain = "**";
- client.Credentials = NetworkCred;
- client.Send(msg);
- }
- catch (Exception ex)
- {
- LogError(ex.InnerException.ToString(), ex.StackTrace);
- }
- }
- public override void Execute(Guid contentDbId)
- {
- System.Diagnostics.Debug.Assert(false);
- System.Diagnostics.Debug.WriteLine(SPContext.Current.Site.WebApplication.Name);
- StringBuilder _emailBody = new StringBuilder();
- string _staffName=string.Empty;
- string _Emailtext=string.Empty;
- string _url =string.Empty;
- string _formNo = string.Empty;
- string _staffEmailAddress = string.Empty;
- try
- {
- string url = @"http://url";
- using (SPSite site = new SPSite(url))
- {
- using (SPWeb web = site.OpenWeb())
- {
- SPListItemCollection coll = web.Lists["test"].Items;
- foreach (SPListItem item in coll)
- {
- if (!string.IsNullOrEmpty(_formStatus))
- {
- if (_formStatus == "Submitted")
- {
- _emailBody.Append("<b>Welcome to Send an Email!!</b><p> Example.<BR>");
- SendEmail(_emailBody.ToString(), _formStatus,_staffEmailAddress);
- }
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- LogError(ex.Source, ex.StackTrace + ex.InnerException.ToString());
- }
- //TODO: write here the code of your job!
- }
- }
- public class TimerJob : SPFeatureReceiver
- {
- const string TASK_LOGGER_JOB_NAME = "Timer Job";
- public override void FeatureActivated(SPFeatureReceiverProperties properties)
- {
- //SPDiagnosticsService diag = new SPDiagnosticsService();
- try
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- SPSite site = properties.Feature.Parent as SPSite;
- // make sure the job isn't already registered
- foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
- {
- if (job.Name == TASK_LOGGER_JOB_NAME)
- job.Delete();
- }
- // install the job
- Reminder.FeatureCode.Test taskLoggerJob = new Reminder.FeatureCode.Test(TASK_LOGGER_JOB_NAME, site.WebApplication);
- SPMinuteSchedule schedule = new SPMinuteSchedule();
- schedule.BeginSecond = 0;
- schedule.EndSecond = 59;
- schedule.Interval = 2;
- //SPSchedule customSchedule = SPSchedule.FromString("daily at 01:45");
- taskLoggerJob.Schedule = schedule;
- taskLoggerJob.Update();
- });
- }
- catch (Exception ex)
- {
- LogError(ex.InnerException.ToString(), ex.StackTrace);
- }
- }
- public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
- {
- SPSite site = properties.Feature.Parent as SPSite;
- // delete the job
- foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
- {
- if (job.Name == TASK_LOGGER_JOB_NAME)
- job.Delete();
- }
- }
- public override void FeatureInstalled(SPFeatureReceiverProperties properties)
- {
- }
- public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement