无法将新项目添加到 ItemAdded 事件接收器中的列表
谁能告诉我为什么这段代码不起作用? “添加代码”本身有效,但不幸的是在 ItemAdded 事件中无效。 我需要在 ItemAdded 事件中使用此代码,因此我无法使用 ItemAdding。
感谢您的任何帮助。
public override void ItemAdded(SPItemEventProperties properties)
{
SPSite site = new SPSite("http://air_sim:39167/");
SPWeb web1 = site.RootWeb;
SPList List = web1.Lists["Announcements"];
SPListItem newitem = List.Items.Add();
newitem["Title"] = "Example";
newitem.Update();
}
Can anybody tell me why this code doesn't work?
The "adding code" itself works, but unfortunately not in an ItemAdded Event.
I need this code in the ItemAdded Event and therefor i cannot use ItemAdding.
Thanks for any help.
public override void ItemAdded(SPItemEventProperties properties)
{
SPSite site = new SPSite("http://air_sim:39167/");
SPWeb web1 = site.RootWeb;
SPList List = web1.Lists["Announcements"];
SPListItem newitem = List.Items.Add();
newitem["Title"] = "Example";
newitem.Update();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
连接调试器。
转到 cmd 并输入 iisapp。您将获得工作进程 ID。
然后打开事件处理程序项目并转到工具和附加进程并在 ItemAdded 以及 ItemAddding 事件上设置调试点
Attach a debugger.
Go to cmd and type iisapp. You would get the worker process id.
Then open your event handler project and go to the tools and attach process and set the debug point on ItemAdded as well as ItemAddding event
请尝试以下解决方案:
<代码>
SPSite site = new SPSite("站点地址");
SPWeb web1 = site.RootWeb;
SPList List = web1.Lists["公告"];
web1.AllowUnsafeUpdates = true;
SPListItem newitem = List.Items.Add();
newitem["标题"] = "示例";
newitem.Update();
web1.AllowUnsafeUpdates = false;
Try the below solutions:
SPSite site = new SPSite("site address");
SPWeb web1 = site.RootWeb;
SPList List = web1.Lists["Announcements"];
web1.AllowUnsafeUpdates = true;
SPListItem newitem = List.Items.Add();
newitem["Title"] = "Example";
newitem.Update();
web1.AllowUnsafeUpdates = false;
您是否执行了任何步骤将事件接收器附加到您的列表?
如果没有,您可以安装一项功能来管理事件接收器和
验证事件接收器是否已添加,如果没有,请手动添加:
http://chrissyblanco.blogspot.com/2007/08/event-receiver -management.html
这样的名称不存在,将抛出异常。另外如果你
不要初始化项目的必填字段,Update() 调用
会抛出异常。
顺便说一句, properties 变量包含许多有用的属性:
Did you do any steps to attach event receiver to your list?
If no, you can install a feature to manage event receivers and
verify that the event receiver is added and if not, add it manually:
http://chrissyblanco.blogspot.com/2007/08/event-receiver-management.html
with such name doesn't exist, exception will be thrown. Also if you
don't initialise required fields of your item, the Update() call
will throw exception.
By the way the properties variable contains many useful properties:
您使用 Sharepoint 2010 还是 Sharepoint2007?
你用的是VS2008还是VS2010?
如果您无法使用调试器,请使用 EventLog:
Do you use Sharepoint 2010 or Sharepoint2007?
Do you use VS2008 or VS2010?
If you couldn’t use debugger, use EventLog: