Sharepoint2007 EventHandler中实现页面跳转

本文探讨了在SharePoint中使用ItemAdding事件而非ItemAdded事件的原因,详细介绍了如何在ItemAdding事件中手工添加列表项并实现页面跳转,以确保列表项能够成功添加。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果你使用ItemAdded, 你会发现你的HttpContext.Current对象是null.ItemAdded和ItemUpdated属于异步(asynchronous )事件,而HttpContext对象仅在同步(synchronous )事件中才可获取.那我们就应该使用ItemAdding了. 可是还有一个问题, 如果你使用ItemAdding事件的话, 你会发现如果执行了跳转, 你的item并不会被加入到列表中!这是因为, 一旦执行了跳转, 那么ItemAdding的线程就会被终止, item也就永远不会添加到list中了.解决方案有点麻烦, 需要你手工地添加你的item, 即在ItemAdding事件中, 手动地拿到SPList对象, 然后执行SPList.Add()方法.

代码:


public class CustomEventReceiver : SPItemEventReceiver
{ 
	private HttpContext _currentContext = null; 
	public CustomEventReceiver () : base ()
 	{ 
		if (null != HttpContext.Current)
		 { 
			_currentContext = HttpContext.Current
		 }
	 } 
	public override void ItemAdding (SPItemEventProperties properties) 
	{
		 // Get a "reference" to the list 
		SPSite siteColl = new SPSite (properties.SiteId); 
		SPWeb site = siteColl.OpenWeb (properties.RelativeWebUrl) ;
		SPList list = site.Lists[properties.ListId]; // Add the item and fill it with the values from properties 
		DisableEventFiring (); 
		SPListItem itemToAdd = list.Items.Add ();
		EnableEventFiring ();
		 // Cleanup 
		site.Dispose ();
		siteColl.Dispose (); 
		// Redirect 
		SPUtility.Redirect (targetUrlOfNewItem, SPRedirectFlags.Default, _currentContext);
 	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值