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.Utilit开发者_高级运维ies;
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.
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
精彩评论