无法将新项目添加到 ItemAdded 事件接收器中的列表

发布于 2024-11-26 22:22:47 字数 483 浏览 1 评论 0原文

谁能告诉我为什么这段代码不起作用? “添加代码”本身有效,但不幸的是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的黑色迷你裙 2024-12-03 22:22:48

连接调试器。
转到 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

猫烠⑼条掵仅有一顆心 2024-12-03 22:22:48

请尝试以下解决方案:

  1. 检查是否存在具有该名称的站点。
  2. 检查用户是否有插入项目的权限。
  3. 尝试使用AllowUnsafeUpdates:
    <代码>
    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:

  1. Check whether the Site exists with that name.
  2. Check whether user has the permission to insert item.
  3. Try using AllowUnsafeUpdates:

    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;
醉梦枕江山 2024-12-03 22:22:47
  1. 您是否执行了任何步骤将事件接收器附加到您的列表?

    如果没有,您可以安装一项功能来管理事件接收器和
    验证事件接收器是否已添加,如果没有,请手动添加:
    http://chrissyblanco.blogspot.com/2007/08/event-receiver -management.html

  2. 也许会抛出异常?例如,如果此类网站或列表
    这样的名称不存在,将抛出异常。另外如果你
    不要初始化项目的必填字段,Update() 调用
    会抛出异常。

顺便说一句, properties 变量包含许多有用的属性:

SPListItem newitem = properties.List.Items.Add();
newitem["Title"] = "Example";
newitem.Update();
  1. 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

  2. Maybe exception is thrown somwere? For example, if such site or list
    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:

SPListItem newitem = properties.List.Items.Add();
newitem["Title"] = "Example";
newitem.Update();
暮年 2024-12-03 22:22:47

您使用 Sharepoint 2010 还是 Sharepoint2007?
你用的是VS2008还是VS2010?
如果您无法使用调试器,请使用 EventLog

public override void ItemAdded(SPItemEventProperties properties)
{
    EventLog.WriteEntry("DebugSharepoint", "ItemAdded fired");
    try
    {
        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();

    }
    catch(Exception e)
    {
        EventLog.WriteEntry("DebugSharepoint", e.Message, EventLogEntryType.Error);        
    }
}

Do you use Sharepoint 2010 or Sharepoint2007?
Do you use VS2008 or VS2010?
If you couldn’t use debugger, use EventLog:

public override void ItemAdded(SPItemEventProperties properties)
{
    EventLog.WriteEntry("DebugSharepoint", "ItemAdded fired");
    try
    {
        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();

    }
    catch(Exception e)
    {
        EventLog.WriteEntry("DebugSharepoint", e.Message, EventLogEntryType.Error);        
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文