下面主要代码为主,一些记录,以备不时只需!
1.新建一个类库项目TaskEventHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using System.IO; namespace EventHandler { public class TaskEventHandler : SPItemEventReceiver { public override void ItemAdded(SPItemEventProperties properties) { //base.ItemAdded(properties); SPListItem taskListItem = properties.ListItem; SPFieldUser assignedTo = (SPFieldUser)taskListItem.Fields[SPBuiltInFieldId.AssignedTo]; SPFieldUserValue user = (SPFieldUserValue)assignedTo.GetFieldValue(taskListItem[SPBuiltInFieldId.AssignedTo].ToString()); SPUser userObject = user.User; using (SPWeb web = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl)) { bool isEmailServerSet = SPUtility.IsEmailServerSet(web); if (isEmailServerSet) { bool appendHtmlTag = false; bool htmlEncode = false; string toAddress = userObject.Email; string subject = "Tasks - " + taskListItem[SPBuiltInFieldId.Title] + " has been assigned to you"; StringBuilder sb = new StringBuilder("" + taskListItem[SPBuiltInFieldId.Title] + "has been assigned to you"); sb.Append("<table>"); sb.Append("<tr><td>Title:</td><td>" + taskListItem["Title"] + "</td></tr>"); sb.Append("<tr><td>Priority:</td><td>" + taskListItem[SPBuiltInFieldId.Priority] + "</td></tr>"); sb.Append("<tr><td>Status:</td><td>" + taskListItem[SPBuiltInFieldId.StatusBar] + "</td></tr>"); sb.Append("<tr><td>% Complete:</td><td>" + taskListItem[SPBuiltInFieldId.Completed] + "</td></tr>"); sb.Append("<tr><td>Assigned To:</td><td>" + taskListItem[SPBuiltInFieldId.AssignedTo] + "</td></tr>"); sb.Append("<tr><td>Description:</td><td>" + taskListItem[SPBuiltInFieldId.DecisionStatus] + "</td></tr>"); sb.Append("<tr><td>Start Date:</td><td>" + taskListItem[SPBuiltInFieldId.StartDate] + "</td></tr>"); sb.Append("<tr><td>Due Date:</td><td>" + taskListItem[SPBuiltInFieldId.Duration] + "</td></tr>"); sb.Append("</table>"); sb.Append("Last Modified " + taskListItem[SPBuiltInFieldId.Modified] + " by " + taskListItem[SPBuiltInFieldId.Modified_x0020_By] + ""); sb.Append("<hr />"); string message = ""; bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message); } SPListItem item = properties.ListItem; string path = @"C:\test.txt"; StreamWriter writer = new StreamWriter(path); writer.WriteLine("My Test"); foreach (SPField f in properties.ListItem.Fields) { writer.WriteLine(f.Title + "==>" + properties.ListItem[f.Title]); } writer.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); writer.WriteLine("SPUtility.IsEmailServerSet(web)==>" + SPUtility.IsEmailServerSet(web)); writer.WriteLine("userObject.Email" + userObject.Email); writer.WriteLine("taskListItem[SPBuiltInFieldId.Title]==>" + taskListItem[SPBuiltInFieldId.Title]); writer.WriteLine("taskListItem[SPBuiltInFieldId.Body]==>" + taskListItem[SPBuiltInFieldId.Body]); writer.WriteLine("ReceiverData==>" + properties.ReceiverData); writer.WriteLine("EmailBody==>" + taskListItem[SPBuiltInFieldId.EmailBody]); writer.WriteLine("EMail==>" + taskListItem[SPBuiltInFieldId.EMail]); writer.WriteLine("Email2==>" + taskListItem[SPBuiltInFieldId.Email2]); writer.WriteLine("Email3==>" + taskListItem[SPBuiltInFieldId.Email3]); writer.WriteLine("EmailBody==>" + taskListItem[SPBuiltInFieldId.EmailFrom]); writer.WriteLine("EmailFrom==>" + taskListItem[SPBuiltInFieldId.EmailSubject]); writer.WriteLine("EmailTo==>" + taskListItem[SPBuiltInFieldId.EmailTo]); writer.WriteLine("EncodedAbsWebImgUrl==>" + taskListItem[SPBuiltInFieldId.EncodedAbsWebImgUrl]); writer.WriteLine("EmailCc==>" + taskListItem[SPBuiltInFieldId.EmailCc]); writer.Close(); } } } }
2.新建一个控制台应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace RevolutionTaskApp
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite(http://mossServer:7000))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists["Tasks"];
//下面第二个参数为上面的DLL程序集信息,可用Reflector查看
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=64e8493434b90339", "EventHandler.TaskEventHandler");
Console.WriteLine("doing ....");
Console.ReadKey();
}
}
}
}
}
3. 运行控制台应用程序即可….
Technorati 标签: EventHandler,moss
记录学习点滴...,坚持每天让自己的技能增加1%,默默的坚持下去吧!:-)