使用Visual Studio创建简单的Event Receiver
事件接收器是添加触发器到SharePoint解决方案的有效方法。
1. 在站点创建TestList列表。
2. 管理员身份打开VS,创建Event Receiver事件接收器。
3. 命名SimpleEventReceiver,部署为场解决方案,点击下一步。
4. 选择列表项事件,Announcements通知列表,正在添加项。
5. 点击完成。
6. 修改SimpleEventReceiver.cs文件,这会在另一个列表中添加新项目:
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace SimpleEventReceiver.SimpleEventReceiver
{
///
/// 列表项事件
///
public class SimpleEventReceiver : SPItemEventReceiver
{
///
/// 正在添加项.
///
public override void ItemAdding(SPItemEventProperties properties)
{
string eventName = "Event List: ";
base.ItemAdding(properties);
logAnAnnouncement(properties, eventName);
}
private void logAnAnnouncement(SPItemEventProperties properties,
string eventName)
{
string listTitle = properties.List.Title;
//Be sure to replace the URL reference below with your
//SharePoint server URL.
string mySiteURL = "http://smallville-pc:1528/";
DateTime currentDate = DateTime.Now;
using (SPSite mySiteCollection = new SPSite(mySiteURL))
{
using (SPWeb mySPSite = mySiteCollection.RootWeb)
{
SPList mySPList = mySPSite.Lists["TestList"];
SPListItem newListItem = mySPList.Items.Add();
newListItem["Title"] = eventName + listTitle + " @ " +
currentDate.ToLongTimeString();
newListItem.Update();
}
}
}
}
}
8. 在Announcements通知列表中,添加新通知。
9. 导航到TestList列表会发现新项目。