【问题标题】:How to update field value in current item via event receiver?如何通过事件接收器更新当前项目中的字段值?
【发布时间】:2012-08-04 23:19:33
【问题描述】:

编辑:我意识到我在第二个代码块中的方法是不必要的。我可以通过在 ItemUpdated 中执行以下操作来完成同样的事情:

SPListItem thisItem = properties.ListItem;

thisItem.File.CheckOut();

thisItem["Facility Number"] = "12345";
thisItem.Update();

thisItem.File.CheckIn("force check in");

不幸的是,当“thisItem.Update();”时我仍然收到相同的错误消息已执行:沙盒代码执行请求被拒绝,因为沙盒代码主机服务太忙,无法处理请求

我实际上在最初部署我的沙盒解决方案并使用此链接时收到了上述错误 (http://blogs.msdn.com/b/sharepointdev/archive/2011/02/08/error-the-sandboxed-code -execution-request-was-refused-because-the-sandboxed-code-host-service-was-too-busy-to-handle-the-request.aspx) 来修复它。


我正在尝试编写一个 C# 事件接收器,它可以在库中添加/更改文档时更改字段的值。我尝试使用以下代码:

public override void ItemUpdating(SPItemEventProperties properties)
{
    base.ItemUpdating(properties);
    string fieldInternalName = properties.List.Fields["Facility Number"].InternalName;
    properties.AfterProperties[fieldInternalName] = "12345";
}

很遗憾,这仅适用于某些领域。例如,如果我将“Facility Number”替换为“Source”,代码将正确执行。这可能是因为我们正在使用第三方软件(称为 KnowledgeLake)将 SharePoint 中的默认编辑表单替换为 Silverlight 表单。无论如何,因为我在上面的代码中遇到了挑战(再次,因为我认为在 ItemUpdating 事件触发后 Silverlight 表单可能会覆盖该字段),我尝试了以下代码:

 public override void ItemUpdated(SPItemEventProperties properties)
 {

       base.ItemUpdated(properties);

       //get the current item
       SPListItem thisItem = properties.ListItem;

       string fieldName = "Facility Number";
       string fieldInternalName = properties.List.Fields[fieldName].InternalName;
       string fieldValue = (string)thisItem["Facility Number"];

       if (!String.IsNullOrEmpty(fieldValue))
       {
           //properties.AfterProperties[fieldInternalName] = "123456789";

           SPWeb oWebsite = properties.Web as SPWeb;
           SPListItemCollection oList = oWebsite.Lists[properties.ListTitle].Items;

           SPListItem newItem = oList.GetItemById(thisItem.ID);

           newItem.File.CheckOut();

           thisItem[fieldInternalName] = "12345";
           thisItem.Update();

           newItem.File.CheckIn("force");
       }
   }

首先,上面的内容对我来说似乎有点笨拙,因为我很想只使用 AfterProperties 方法。此外,执行“newItem.Update()”时出现以下错误:他的沙盒代码执行请求被拒绝,因为沙盒代码主机服务太忙而无法处理该请求

我在这里遗漏了什么吗?我很想利用第一个代码块。任何帮助将不胜感激。

【问题讨论】:

  • 想通了。我不得不用 base.EventFiringEnabled = false; 包装我的代码。 ... 运行代码 ... base.EventFiringEnabled = true;
  • 成功了,谢谢。
  • @Josh - 所以请把它写成答案,以帮助遇到这个问题的任何人。

标签: sharepoint event-receiver


【解决方案1】:

乔希能够回答他自己的问题,这也帮助我解决了我的问题。这是一个有效的代码片段。

public override void ItemUpdated(SPItemEventProperties properties)
{
 string internalName = properties.ListItem.Fields[columnToUpdate].InternalName;

 //Turn off event firing during item update
 base.EventFiringEnabled = false;

 SPListItem item = properties.ListItem;
 item[internalName] = newVal;
 item.Update();

 //Turn back on event firing
 base.EventFiringEnabled = true;

 base.ItemUpdated(properties);
}

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    相关资源
    最近更新 更多