先說一下這個項目做了什么。先介紹一下背景(備注一下,每次項目發生更改之后,要進行clean 和rebuild兩個操作,否則最新的更改保存不到exe文件中,這樣上線后的系統還是執行得原有的已編譯過的程序代碼,所以得出的結論就是在上線測試階段,對于程序代碼的每一次更改都要rebuild操作,來保證每次的更改作用于exe文件,保證編譯是最新的;;)
上面這個流程圖介紹了當我們需要take training follow-up的時候,我們需要在sharepoint Desinger中設置工作流,來完成這些功能,但是現在遇到一個問題,workflow的觸發是由于登錄頁面的用戶log一條item記錄的時候觸發的,如果我們要實現郵件的定時提醒功能,那么就需要在每天都觸發執行一次workflow,但是由誰來觸發呢?剛開始網上搜了很多資料,內網 外網資料都搜了一圈,好不容易看到一個帖子說可以實現,但最后試著用workflow來解決,但是始終行不通,就算讓它執行個15分鐘,過了會兒就會掛掉。我估計系統可能開著這個線程太占用系統資源了。最終逼不得已才決定使用web service來解決這個問題,但是問題又來了,對于java還算了解,但是對C#還是完全陌生,如果采用C#來做這個定時發送郵件的功能的話,帶來的risk無法估量,而且極有可能造成PRoject的delay,所以在前期的決策上,試著采用了Java做了一部分調研,但是考慮到公司的sharepoint是部署位置及其Java與windows底層通信協議可能帶來的risk:項目進行到后期,極有可能無法完成,所以最終決定用C#來實現這個功能。。
該學習的技術還是要學的,你越逃避的事情,它越會找上你。所以,學習新技術也要趁早,等你的項目中需要用到的時候,你已經可以游刃有余了。
如上,為C#程序中要實現的邏輯。程序的整體實現邏輯是這樣的。我們取出list列表,然后進行過濾,當滿足我們的要求的時候,我們發郵件,然后做一些異常處理。Servic.cs代碼如下:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading.Tasks;using System.Net;using System.xml;using System.Timers;using System.Configuration;//using System.Web.Mail;using System.Net.Mail;namespace SharePointWorklow{ public partial class Service1 : ServiceBase { //static private long FIVE_DAYS = 432000000L; public Service1() { InitializeComponent(); } public void Debug(string[] args) { this.OnStart(args); } protected override void OnStart(string[] args) { //Debugger.Launch(); System.Timers.Timer TimeCheck = new System.Timers.Timer(); TimeCheck.Interval = Double.Parse(ConfigurationSettings.AppSettings["INTERVAL"]); //TimeCheck.Interval = 60000; TimeCheck.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick); TimeCheck.AutoReset = true; TimeCheck.Enabled = true; timer1_Tick(this, ElapsedEventArgs.Empty); TimeCheck.Start(); } public static String MailBody(XmlNode node, Double delayDays) { String URL = @"http://ent261.sharepoint.hp.com/teams/jupiter_2/Lists/Training%20Record%20FY14/MyItems.aspx"; String start = "Congratulations for completing your training course: /n/nCourse Name: " + node.Attributes["ows_LinkTitle"].Value + ";/n"; String time = "Time: " + node.Attributes["ows_CreateTime"].Value; if (node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"] != null) { time = "From: " + node.Attributes["ows_CreateTime"].Value + "; /nTo: " + node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value + ";/n/n"; } String mention = "We mentioned you have not finished your follow-up with your PL for " + (int)delayDays + " days./n"; String end = "Please check this link for finishing the following up of this course: /n"; String mailBody = start + time + mention + end + URL; return mailBody; } /* public static void SendE_Mail(XmlNode node, Double day, String name, String ccName, String fromName) { String time = "Time: " + node.Attributes["ows_CreateTime"].Value; String deadline = ""; String deadline1 = ""; if (node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"] != null) //未修改過的training按照end time計算deadline { DateTime a = Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value).AddDays(42); DateTime b= new DateTime(2015,3,31); if (Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value) < new DateTime(2015,4,1) && Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value) > new DateTime(2015, 3, 1)) { //判斷training是否是3月-4月的training a = b.AddDays(42); } deadline = "<b>before " + a.ToShortDateString().ToString() + @" </b>"; deadline1 = "before " + a.ToShortDateString().ToString() + @""; time = "From: <b>" + node.Attributes["ows_CreateTime"].Value + "</b> to: <b>" + node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value + "</b><Br /><Br />"; } if(node.Attributes["ows_Update_x0020_Date"] != null) //修改過的training按照修改日期計算deadline { if (Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value) > Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value)) { DateTime a = Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value).AddDays(21); //Console.WriteLine(a.ToShortDateString()); deadline = "<b>before " + a.ToShortDateString().ToString() + @" </b>"; deadline1 = "before " + a.ToShortDateString().ToString() + @""; } } //string ls_Subject = "" + (String)k2 + ", " + (String)k1 + @" Takes " + node.Attributes["ows_LinkTitle"].Value + @""; string ls_Subject = "" + node.Attributes["ows_LinkTitle"].Value + @"Training Follow-up Reminder: Required Completion Date "+ deadline1 +@""; SmtpMail.SmtpServer = "smtp-americas.hp.com"; MailMessage lo_Message = new MailMessage(); lo_Message.From = "no-reply@hp.com"; //lo_Message.To = node.Attributes["ows_Name"].Value.Substring(node.Attributes["ows_Name"].Value.IndexOf('#') + 1); lo_Message.Cc = "xu-guang.zhao@hp.com"; lo_Message.Subject = ls_Subject; lo_Message.Priority = MailPriority.High; lo_Message.Body = @"<html> <body> <table border='0'> <tr> <font face='HP Simplified' size='小四'><td>Hello " + (String)name + @"</td></font> </tr> <tr> <td> </td> </tr> <tr> <font face='HP Simplified' size='小四'> <td>This is a kind reminder that you have not completed your training follow-up with your PM for <b>" + (int)day + @" days</b>.Please follow the link below and take actions " + deadline + @". </td> </font> </tr> <tr> <td> </td> </tr> <tr> <font face='HP Simplified' size='小四'> <td>Course Name: <b>" + node.Attributes["ows_LinkTitle"].Value + @"</b></td> </font> </tr> <tr><font face='HP Simplified' size='小四'><td>" + time + @"</td></font> </tr> <tr> <td><a href=" + @"http://ent261.sharepoint.hp.com/teams/jupiter_2/Lists/Training%20Record%20FY14/MyItems.aspx" + @"><font color='blue' face='HP Simplified' size='小四'>My Training Items</font></a></td> </tr> <tr> <font color='blue' face='HP Simplified' size='小四'>*Complete training follow-up in time is a mandatory action, according to Jupiter ground rules*</font> </tr> <br/><br/><br/><br/> </td> <font color='red' face='HP Simplified'
新聞熱點
疑難解答