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?

  • what version of sharepoint are you using? – Mike Dec 17 '14 at 21:56
  • SharePoint 2013 on-premise – Andres Williams Dec 17 '14 at 21:59
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)

  • 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?.. – Andres Williams Dec 18 '14 at 14:26
  • both are "anonymous". I'm not sure what you are asking; your English is very broken. – Mike Dec 18 '14 at 15:26
  • sorry, I know my english is very broken, you answer is correct, thanks! :) – Andres Williams Dec 18 '14 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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