{"id":4337,"date":"2012-08-30T02:05:21","date_gmt":"2012-08-30T02:05:21","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/optionally-running-spsecurity-runwithelevatedprivileges-with-delgates\/"},"modified":"2016-07-28T10:51:47","modified_gmt":"2016-07-28T10:51:47","slug":"optionally-running-spsecurity-runwithelevatedprivileges-with-delgates","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/optionally-running-spsecurity-runwithelevatedprivileges-with-delgates\/","title":{"rendered":"Optionally Running SPSecurity.RunWithElevatedPrivileges with Delgates"},"content":{"rendered":"<p>I was writing some SharePoint code today where I needed to give people the option of running some code with elevated permission.&#160; When you run code in an elevated fashion it normally looks like this:<\/p>\n<p>SPSecurity.RunWithElevatedPrivileges(()=&gt;   <br \/>{    <br \/>&#160;&#160;&#160; \/\/Code to run    <br \/>});<\/p>\n<p>It wasn&#8217;t a lot of code so I was initially inclined to do something horrible like this:<\/p>\n<p>public void SomeMethod(bool runElevated)   <br \/>{    <br \/>&#160;&#160;&#160; if(runElevated)    <br \/>&#160;&#160;&#160; {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; SPSecurity.RunWithElevatedPrivileges(()=&gt;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Code to run    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; });    <br \/>&#160;&#160;&#160; }    <br \/>&#160;&#160;&#160; else    <br \/>&#160;&#160;&#160; {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Copy of code to run    <br \/>&#160;&#160;&#160; }    <br \/>}<\/p>\n<p>Easy enough, but I did not want to draw the ire of my coworkers for employing the CTRL+C CTRL+V design pattern.&#160; Extracting the code into a whole new method would have been overkill because it was a pretty brief piece of code.&#160; But then I thought, hey, wait, I&#8217;m basically just running a delegate, so why not define the delegate once and run it either in an elevated context or stand alone, which resulted in this version which I think is much cleaner because the code is only defined once and it didn&#8217;t require a bunch of extra lines of code to define a method:<\/p>\n<p>public void SomeMethod(bool runElevated)   <br \/>{    <br \/>&#160;&#160;&#160; var code = new SPSecurity.CodeToRunElevated(()=&gt;    <br \/>&#160;&#160;&#160; {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Code to run    <br \/>&#160;&#160;&#160; });    <br \/>&#160;&#160;&#160; if(runElevated)    <br \/>&#160;&#160;&#160; {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; SPSecurity.RunWithElevatedPermissions(code);&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160; }    <br \/>&#160;&#160;&#160; else    <br \/>&#160;&#160;&#160; {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Code();    <br \/>&#160;&#160;&#160; }    <br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was writing some SharePoint code today where I needed to give people the option of running some code with elevated permission.&#160; When you run code in an elevated fashion it normally looks like this: SPSecurity.RunWithElevatedPrivileges(()=&gt; { &#160;&#160;&#160; \/\/Code to run }); It wasn&#8217;t a lot of code so I was initially inclined to do&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-4337","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/4337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=4337"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/4337\/revisions"}],"predecessor-version":[{"id":42221,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/4337\/revisions\/42221"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=4337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=4337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=4337"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=4337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}