为什么这个事件接收器代码不起作用?
我们正在尝试创建一个 ItemAdded 事件接收器,它将更新自定义 SharePoint 列表中的创建者(作者)字段。在此自定义列表中,我们启用了 Item-Lever Permissions,以便 userA 只能看到他们创建的内容。问题是,当另一个用户 (UserB) 为其他人 (UserA) 创建项目时,用户 A 将无法看到该项目。
因此,我们希望将“请求者”字段中的所有内容复制到“创建者”字段中。为了实现这一目标,在网上少数人的帮助下,我们创建了以下事件接收器,但它不起作用。你能告诉我们有什么问题吗?
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、SPUserCodeExecutionContextexecutionContext)
在 Microsoft.SharePoint.UserCode.SPUserCodeExecutionManager.Execute(类型 userCodeWrapperType、SPSite 站点、SPUserCodeExecutionContextexecutionContext)
在createdByElevate中加载和运行事件接收器createdByElevate.EventReceiver1.EventReceiver1时出错,Version=1.0.0.0,Culture=neutral,PublicKeyToken=97fddd01b051f985。附加信息如下。服务器遇到内部错误。有关详细信息,请关闭服务器的 .config 文件中的 customErrors。
- 沙盒代码执行请求失败。 - 内部异常:System.Runtime.Remoting.RemotingException:服务器遇到内部错误。有关详细信息,请关闭服务器的 .config 文件中的 customErrors。服务器堆栈跟踪:异常重新抛出 在 [0]:
We are trying to create an ItemAdded event receiver that will update Created By (Author) field in a custom SharePoint list. In this custom list we have enabled Item-Lever Permissions so that userA will only be able to see what they create. Issue is when another User (UserB) creates the item for someone else (UserA), User A will not be able to see that item.
So we want whatever is in Request By filed to be copied to Created By field. To get there, with the help of few people online, we have created the following event receiver but it does not work. Can you tell us whats wrong with it?
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();
}
});
}
}
}
Found the following error in ULS logs:
- Sandboxed code execution request failed. - Inner Exception: System.Runtime.Remoting.RemotingException: Server encountered an internal error. For more information, turn off customErrors in the server's .config file. Server stack trace: Exception rethrown
at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Microsoft.SharePoint.Administration.ISPUserCodeExecutionHostProxy.Execute(Type userCodeWrapperType, Guid siteCollectionId, SPUserToken userToken, String affinityBucketName, SPUserCodeExecutionContext executionContext)
at Microsoft.SharePoint.UserCode.SPUserCodeExecutionManager.Execute(Type userCodeWrapperType, SPSite site, SPUserCodeExecutionContext executionContext)
Error loading and running event receiver createdByElevate.EventReceiver1.EventReceiver1 in createdByElevate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=97fddd01b051f985. Additional information is below. Server encountered an internal error. For more information, turn off customErrors in the server's .config file.
- Sandboxed code execution request failed. - Inner Exception: System.Runtime.Remoting.RemotingException: Server encountered an internal error. For more information, turn off customErrors in the server's .config file. Server stack trace: Exception rethrown
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的错误似乎表明您已将其部署为沙盒解决方案。遗憾的是,您无法在此类部署中使用提升的权限 (
SPSecurity.RunWithElevatedPrivileges
)。您必须考虑另一种方法来解决此限制,或者重新部署为场解决方案。Your error seems to suggest that you have deployed this as a Sandboxed solution. Unfortunately you can't use elevated privileges (
SPSecurity.RunWithElevatedPrivileges
) in this type of deployment. You will either have to think of another way around this limitation or redeploy as a Farm solution.您可以首先验证“提交用户”和“请求者”列是否具有相同的数据类型。
我的意思是,如果它们的类型相同,那么它会工作得很好。
谢谢,
-桑托什
Can you first verify whether both "Submit User" and "Requested By" columns are of same datatype.
I mean if they were of same Type then it will work fine.
Thanks,
-Santosh