Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. http://www.andrewconnell.com/Creating-Custom-SharePoint-Timer-Jobs.
  2.  
  3. }
  4. public Test(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType)
  5. {
  6. this.Title = "Timer Job";
  7. }
  8. public Test(string jobName, SPWebApplication webApplication)
  9. : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
  10. {
  11.  
  12. this.Title = "Timer Job";
  13.  
  14. }
  15.  
  16. private void SendEmail(string EmailBody, string Subject, string ToEmailAddress)
  17. {
  18. try
  19. {
  20. System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
  21.  
  22. msg.To.Add(ToEmailAddress);
  23. msg.From = new System.Net.Mail.MailAddress("****");
  24. msg.Subject = Subject;
  25.  
  26. msg.IsBodyHtml = true;
  27. //EmailBody = " <b>Welcome to Send an Email!!</b><p> Example.<BR>";
  28. msg.Body = EmailBody;
  29. string smtp = "*****";
  30. System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtp);
  31. System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
  32. NetworkCred.UserName = "**";
  33. NetworkCred.Password = "**";
  34. NetworkCred.Domain = "**";
  35. client.Credentials = NetworkCred;
  36. client.Send(msg);
  37. }
  38. catch (Exception ex)
  39. {
  40.  
  41. LogError(ex.InnerException.ToString(), ex.StackTrace);
  42. }
  43.  
  44. }
  45.  
  46.  
  47.  
  48. public override void Execute(Guid contentDbId)
  49. {
  50. System.Diagnostics.Debug.Assert(false);
  51. System.Diagnostics.Debug.WriteLine(SPContext.Current.Site.WebApplication.Name);
  52. StringBuilder _emailBody = new StringBuilder();
  53. string _staffName=string.Empty;
  54. string _Emailtext=string.Empty;
  55. string _url =string.Empty;
  56. string _formNo = string.Empty;
  57. string _staffEmailAddress = string.Empty;
  58. try
  59. {
  60. string url = @"http://url";
  61. using (SPSite site = new SPSite(url))
  62. {
  63. using (SPWeb web = site.OpenWeb())
  64. {
  65. SPListItemCollection coll = web.Lists["test"].Items;
  66. foreach (SPListItem item in coll)
  67. {
  68.  
  69. if (!string.IsNullOrEmpty(_formStatus))
  70. {
  71. if (_formStatus == "Submitted")
  72. {
  73.  
  74. _emailBody.Append("<b>Welcome to Send an Email!!</b><p> Example.<BR>");
  75. SendEmail(_emailBody.ToString(), _formStatus,_staffEmailAddress);
  76.  
  77. }
  78.  
  79. }
  80.  
  81. }
  82. }
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87.  
  88. LogError(ex.Source, ex.StackTrace + ex.InnerException.ToString());
  89. }
  90.  
  91.  
  92. //TODO: write here the code of your job!
  93.  
  94. }
  95.  
  96. }
  97. public class TimerJob : SPFeatureReceiver
  98. {
  99. const string TASK_LOGGER_JOB_NAME = "Timer Job";
  100. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  101. {
  102. //SPDiagnosticsService diag = new SPDiagnosticsService();
  103.  
  104.  
  105. try
  106. {
  107. SPSecurity.RunWithElevatedPrivileges(delegate()
  108. {
  109. SPSite site = properties.Feature.Parent as SPSite;
  110.  
  111. // make sure the job isn't already registered
  112. foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
  113. {
  114. if (job.Name == TASK_LOGGER_JOB_NAME)
  115. job.Delete();
  116. }
  117.  
  118. // install the job
  119. Reminder.FeatureCode.Test taskLoggerJob = new Reminder.FeatureCode.Test(TASK_LOGGER_JOB_NAME, site.WebApplication);
  120.  
  121. SPMinuteSchedule schedule = new SPMinuteSchedule();
  122. schedule.BeginSecond = 0;
  123. schedule.EndSecond = 59;
  124. schedule.Interval = 2;
  125.  
  126. //SPSchedule customSchedule = SPSchedule.FromString("daily at 01:45");
  127.  
  128. taskLoggerJob.Schedule = schedule;
  129.  
  130. taskLoggerJob.Update();
  131. });
  132. }
  133. catch (Exception ex)
  134. {
  135.  
  136. LogError(ex.InnerException.ToString(), ex.StackTrace);
  137.  
  138. }
  139. }
  140.  
  141. public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  142. {
  143. SPSite site = properties.Feature.Parent as SPSite;
  144. // delete the job
  145. foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
  146. {
  147. if (job.Name == TASK_LOGGER_JOB_NAME)
  148. job.Delete();
  149. }
  150. }
  151.  
  152. public override void FeatureInstalled(SPFeatureReceiverProperties properties)
  153. {
  154.  
  155. }
  156.  
  157. public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
  158. {
  159.  
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement