Visual Studio创建简单的Event Receiver

使用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();
                }
            }
        }
    }
}
7. 部署。
8. 在Announcements通知列表中,添加新通知。
9. 导航到TestList列表会发现新项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值