2

SharePoint 2013 on-premise

What is the difference of using:

SPSecurity.RunWithElevatedPrivileges(() =>
{
    using (SPWeb web = site.OpenWeb())
    {
        ....
    }
}

with

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPWeb web = site.OpenWeb())
    {
        ....
    }
}

the first code not work, but the second code if it works.

what is the difference between the second code works and the another not?

2
  • what version of sharepoint are you using?
    – Mike
    Commented Dec 17, 2014 at 21:56
  • SharePoint 2013 on-premise Commented Dec 17, 2014 at 21:59

1 Answer 1

3

Code wizards, correct me if I'm wrong, but I think the first is a lamda which basically states this anonymous function is bound anonymously, but is not instantiated. The second is a delegate that is also instantiated. It's the same as saying:

MyHelloDelegateType2 Hello3 = delegate {
 MessageBox.Show("Default message!");
};

but you need to instantiate it, it's just creating a delegate object.

Hello3("Test");

So:

SPSecurity.RunWithElevatedPrivileges(Delegate object) vs SPSecurity.RunWithElevatedPrivileges(Delegated code)

3
  • ok, the SPSecurity.RunWithElevatedPrivileges(() => create object anonymous and is executed in other context but the SPSecurity.RunWithElevatedPrivileges(delegate() needs to be executed in the current context and not allow objects to be anonymous. ok?.. Commented Dec 18, 2014 at 14:26
  • both are "anonymous". I'm not sure what you are asking; your English is very broken.
    – Mike
    Commented Dec 18, 2014 at 15:26
  • sorry, I know my english is very broken, you answer is correct, thanks! :) Commented Dec 18, 2014 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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