SharePoint列表事件接收器(Event Receiver)是一种机制,允许开发者在SharePoint列表上执行自定义代码,以响应特定的事件,如项添加、更新或删除。事件接收器可以在服务器端或客户端运行。
未在SharePoint列表上触发EventReceiver
.xml
或.ascx
)已正确部署。以下是一个简单的服务器端事件接收器示例,用于在SharePoint列表项添加时发送通知邮件:
using Microsoft.SharePoint;
using System;
using System.Net.Mail;
namespace SharePointEventReceiverProject.EventReceivers
{
public class ItemAddedEventReceiver : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
// 获取列表项信息
string itemTitle = properties.ListItem.Title;
string itemUrl = properties.ListItem.Url;
// 发送通知邮件
SendNotificationEmail(itemTitle, itemUrl);
}
private void SendNotificationEmail(string itemTitle, string itemUrl)
{
string fromAddress = "noreply@example.com";
string toAddress = "admin@example.com";
string subject = "New Item Added: " + itemTitle;
string body = "A new item has been added to the SharePoint list: " + itemTitle + "\nURL: " + itemUrl;
MailMessage mailMessage = new MailMessage(fromAddress, toAddress, subject, body);
SmtpClient smtpClient = new SmtpClient("smtp.example.com");
try
{
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
// 记录错误日志
Console.WriteLine("Error sending email: " + ex.Message);
}
}
}
}
希望这些信息能帮助你解决SharePoint列表事件接收器未触发的问题。
领取专属 10元无门槛券
手把手带您无忧上云