转:http://blog.csdn.net/alfred_72/article/details/9980279
因为不知道VS2010 Sharepoint 有EventReciver这个添加项,走了很多弯路。
步骤:
1.建立 空白Sharepoint项目解决方案。
2.添加项 选择-EventReciver(事件接收器)。
3. 配置--事件接收器类型,事件源项,需要处理哪些事件。
完成之后会生成相应的EventHandler类,方法,Feature1.feature1会自动添加站点功能项。 可以查看一下feature.xml和Element都生成了什么东西,这两个是部署到Sharepoint站点上的关键东西,一开始我不知道可以自动生成,自己在那写这两个文件,一直部署不了。
接着是重写相应的类中的方法,我这里是重写ItemAdded方法,在list 添加新列的时候发送邮件通知。
Code:
- using System;
- using System.Security.Permissions;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Security;
- using Microsoft.SharePoint.Utilities;
- using Microsoft.SharePoint.Workflow;
- using System.Net;
- using System.Net.Mail;
- using System.Net.Mime;
- using System.Text;
- namespace SharePointProject4.EventReceiver1
- {
- /// <summary>
- /// 列表项事件
- /// </summary>
- public class EventReceiver1 : SPItemEventReceiver
- {
- /// <summary>
- /// 已添加项.
- /// </summary>
- public override void ItemAdded(SPItemEventProperties properties)
- {
- base.ItemAdded(properties);
- string mailTitle = "";
- string mailContent = "";
- string address = "";
- if (properties.ListTitle == "邮件内容")
- {
- SPListItem item = properties.ListItem;
- mailTitle = item["标题"].ToString();
- mailContent = item["邮件内容"].ToString();
- address = item["邮件地址"].ToString();
- }
- SendEmail(mailTitle, mailContent, address);
- }
- public void SendEmail(string title, string content, string address)
- {
- try
- {
- Encoding encoding = Encoding.GetEncoding(936);
- MailMessage Message = new MailMessage(
- //from
- new MailAddress("117738414@qq.com", "邮件主题", encoding),
- //to
- new MailAddress(address));
- Message.SubjectEncoding = encoding;
- Message.Subject = title;
- Message.BodyEncoding = encoding;
- Message.Body = content;
- //Message.Attachments.Add(new Attachment(null));
- SmtpClient smtpClient = new SmtpClient("smtp.qq.com");
- smtpClient.Credentials = new NetworkCredential("2313626895", "password");
- smtpClient.Timeout = 20000;
- smtpClient.Send(Message);
- }
- catch (Exception ex)
- {
- }
- }
- }
- }
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text; namespace SharePointProject4.EventReceiver1
{
/// <summary>
/// 列表项事件
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// 已添加项.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties); string mailTitle = "";
string mailContent = "";
string address = ""; if (properties.ListTitle == "邮件内容")
{
SPListItem item = properties.ListItem;
mailTitle = item["标题"].ToString();
mailContent = item["邮件内容"].ToString();
address = item["邮件地址"].ToString(); } SendEmail(mailTitle, mailContent, address); } public void SendEmail(string title, string content, string address)
{
try
{
Encoding encoding = Encoding.GetEncoding(936); MailMessage Message = new MailMessage(
//from
new MailAddress("117738414@qq.com", "邮件主题", encoding),
//to
new MailAddress(address)); Message.SubjectEncoding = encoding;
Message.Subject = title;
Message.BodyEncoding = encoding;
Message.Body = content;
//Message.Attachments.Add(new Attachment(null));
SmtpClient smtpClient = new SmtpClient("smtp.qq.com");
smtpClient.Credentials = new NetworkCredential("2313626895", "password");
smtpClient.Timeout = 20000;
smtpClient.Send(Message);
}
catch (Exception ex)
{ } } }
}