Welcome

首页 / 软件开发 / .NET编程技术 / 在VS中创建简单的Event Receiver

在VS中创建简单的Event Receiver2015-01-01事件接收器是添加触发器到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{/// <summary>/// 列表项事件/// </summary>public class SimpleEventReceiver : SPItemEventReceiver{/// <summary>/// 正在添加项./// </summary>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();}}}}}
7. 部署。

8. 在Announcements通知列表中,添加新通知。

9. 导航到TestList列表会发现新项目。