Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In sharepoint 2010 Foundation, after we installed a simple event receiver that mainly runs when certain items are added/modified, we noticed, during manual testing, that if we logged in as a user other than the sharepoint administrator account we would get an error whenever we tried retrieving to retrieve a simple lookup column information. The error Message said

Error loading and running event receiver      EvozonAdministrationEventReceivers.StartEmailNotification.StartEmailNotification in   EvozonAdministrationEventReceivers, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=918ea4bf742b9a3a. Additional information is below.
: Value does not fall within the expected range.

The same code run on Dev or with the Administrator logged in had no issues running.

Does anybody have any idea why. The server has Active Directory installed. It makes absolutely no sense to me. Are lookups executed in some type of sandbox?

Partial code listing below:

var Field = FieldResolver.On(_onBoardingChecklist);
        SPSecurity.RunWithElevatedPrivileges(() =>
        {
            string computerType = LookupValue<Columns.ComputerType>();
 // .. omitted for previty

public string LookupValue<SITECOLUMN>() where SITECOLUMN : new()
    {
        if (_Field == null) 
            _Field = FieldResolver.On(_onBoardingChecklist);

        var value = (string)_onBoardingItem[_Field.For<SITECOLUMN>().InternalName];

        value = new SPFieldLookupValue(value).LookupValue;

        return value;
    }
share|improve this question
    
Please add the omitted code inside your RWEP code to help solve this –  Anders Rask Apr 23 '11 at 21:10

1 Answer 1

up vote 1 down vote accepted

Looks like you have a piece of code in your event receiver, which requires elevated privilegies. You should use SPSecurity.RunWithElevatedPrivilegies for this piece of code.

Update:

Also, please make sure you have recreated all SharePoint objects inside the RunWithElevatedPrivilegies delegate, because, for example, this would work:

SPSecurity.RunWithElevatedPrivileges(() =>
{
    using (SPSite site = new SPSite(properties.SiteId))
    {
        using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
        {
            // ...
            web.Update();
        }
    }
});

But this, wouldn't:

SPSecurity.RunWithElevatedPrivileges(() =>
{
    // ...
    properties.Web.Update();
});

Thanks Anders Rask for the important note about SPContext!

share|improve this answer
    
I'm already running with elevated privileges. –  Mihai Lazar Apr 22 '11 at 8:56
    
RunWithElevatedPrivilegies has some specifics. You should create all the SP objects inside it (starting from the SPSite), if you create them outside, this function does nothing. Could you place all of your receiver code into RunWithElevatedPrivilegies block, just for test? –  Andrey Markeev Apr 23 '11 at 20:12
    
you can't blindly run everything with elevated privileges because your tests fail when not logged in as an administrator. –  Karthik Murugesan Apr 23 '11 at 20:43
    
Probably your list containing lookup values has unique permissions. You cannot use SPContext inside event receier though, you would need to use Properties inside RWEP: SPSite site = new SPSite(properties.SiteId); SPWeb web = site.OpenWeb(properties.RelativeWebUrl); SPList list = web.Lists[properties.ListId]; SPListItem item = list.Items.GetItemById(properties.ListItemId); –  Anders Rask Apr 23 '11 at 21:07
    
@Karthik Murugesan, agreed, that's why I wrote just for test :) –  Andrey Markeev Apr 23 '11 at 22:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.