1

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
user973640
  • 11
  • 3
  • are you sure the event receiver is attached and actually running? Is there anything in the ULS or Windows event logs when this runs? When you debug the code does it throw any exceptions? You'll have to help us out a bit and give us some more info. – SHug Oct 03 '11 at 12:11
  • I think I have attached it correctly, in Visual Studio I would click on F5 and attach and test. it does not throw any error that I can see. I will double check on ULS logs right now – user973640 Oct 03 '11 at 13:03
  • Found two errors in ULS: – user973640 Oct 03 '11 at 13:12

2 Answers2

1

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.

SHug
  • 688
  • 8
  • 15
0

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

  • Yes, they are the same type (Person/Group), Submit User is same as Created By or Author, I've just labeled it differently – user973640 Oct 03 '11 at 12:57