【问题标题】:Why wont this event receiver code work?为什么这个事件接收器代码不起作用?
【发布时间】:2015-01-13 03:50:22
【问题描述】:

我们正在尝试创建一个 ItemAdded 事件接收器,它将更新自定义 SharePoint 列表中的创建者(作者)字段。在这个自定义列表中,我们启用了 Item-Lever Permissions,以便用户 A 只能看到他们创建的内容。问题是当另一个用户 (UserB) 为其他人 (UserA) 创建项目时,用户 A 将无法看到该项目。

因此,我们希望将 Request By 字段中的任何内容复制到 Created By 字段。为了到达那里,在少数在线人的帮助下,我们创建了以下事件接收器,但它不起作用。你能告诉我们它有什么问题吗?

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace createdByElevate.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           //update base first
           base.ItemAdded(properties);
           string SiteUrl = properties.Web.Url;
           SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               using (SPSite site = new SPSite(SiteUrl))
               {
                   SPWeb web = site.OpenWeb();
                   SPList List = web.Lists["listName"];
                   SPListItem item = List.GetItemById(properties.ListItem.ID);
                   item["Submit User"] = item["Requested By"];
                   item.Update();

               }

           });



       }


    }
}

在 ULS 日志中发现以下错误:

    • 沙盒代码执行请求失败。 - 内部异常:System.Runtime.Remoting.RemotingException:服务器遇到内部错误。有关详细信息,请在服务器的 .config 文件中关闭 customErrors。服务器堆栈跟踪:重新抛出异常 在 [0]:
      在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
      在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 类型)
      在 Microsoft.SharePoint.Administration.ISPUserCodeExecutionHostProxy.Execute(类型 userCodeWrapperType、Guid siteCollectionId、SPUserToken userToken、字符串 affinityBucketName、SPUserCodeExecutionContext executionContext)
      在 Microsoft.SharePoint.UserCode.SPUserCodeExecutionManager.Execute(键入 userCodeWrapperType,SPSite 站点,SPUserCodeExecutionContext executionContext)

    在 createdByElevate 中加载和运行事件接收器 createdByElevate.EventReceiver1.EventReceiver1 时出错,Version=1.0.0.0,Culture=neutral,PublicKeyToken=97fddd01b051f985。附加信息如下。服务器遇到内部错误。如需更多信息,请在服务器的 .config 文件中关闭 customErrors。

【问题讨论】:

  • 您确定事件接收器已连接并实际运行吗?运行时 ULS 或 Windows 事件日志中是否有任何内容?当您调试代码时,它会抛出任何异常吗?你得帮我们一点忙,给我们一些更多的信息。
  • 我想我已经正确地附加了它,在 Visual Studio 中我会点击 F5 并附加并测试。它不会抛出任何我可以看到的错误。我现在会仔细检查 ULS 日志
  • 在 ULS 中发现两个错误:

标签: c# sharepoint event-receiver


【解决方案1】:

您的错误似乎表明您已将其部署为沙盒解决方案。不幸的是,您不能在这种类型的部署中使用提升的权限 (SPSecurity.RunWithElevatedPrivileges)。您将不得不考虑另一种解决此限制的方法,或者重新部署为农场解决方案

【讨论】:

  • 好的,我将尝试重新部署为场解决方案。但是每个人都同意这段代码应该可以工作吗?
【解决方案2】:

您能否首先验证“提交用户”和“请求者”列是否具有相同的数据类型。 我的意思是,如果它们的类型相同,那么它会正常工作。

谢谢, -桑托什

【讨论】:

  • 是的,它们是同一类型(个人/组),提交用户与创建者或作者相同,我只是标记不同
猜你喜欢
  • 1970-01-01
  • 2018-11-05
  • 2012-02-22
  • 2023-03-03
  • 2017-10-02
  • 2016-07-10
  • 2010-12-14
  • 2017-04-09
  • 2013-01-30
相关资源
最近更新 更多