{"id":1645,"date":"2014-02-25T15:51:22","date_gmt":"2014-02-25T15:51:22","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1645"},"modified":"2014-02-25T15:51:22","modified_gmt":"2014-02-25T15:51:22","slug":"quick-tip-20","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/","title":{"rendered":"Run With Elevated Privilges"},"content":{"rendered":"<p><strong>SharePoint Short #20<\/strong><\/p>\n<p>Instead of explicitly wrapping code in a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx\" title=\"SPSecurity.RunWithElevatedPrivileges method\" target=\"_blank\">RunWithElevatedPrivileges<\/a> block, it&#8217;s better to first check if the user has sufficient privileges and only elevate when they don&#8217;t.<\/p>\n<p>Instead of:<\/p>\n<pre lang=\"csharp\">\r\nSPSecurity.RunWithElevatedPrivileges(() =>\r\n    {\r\n        \/\/ elevated code...\r\n    });\r\n<\/pre>\n<p>Do this:<\/p>\n<pre lang=\"csharp\">\r\npublic void RunWithElevatedPrivileges(SPWeb web, SPSecurity.CodeToRunElevated elevatedCode)\r\n{\r\n    if (web.CurrentUser.IsSiteAdmin)\r\n    {\r\n        elevatedCode();\r\n        return;\r\n    }\r\n    SPSecurity.RunWithElevatedPrivileges(elevatedCode);\r\n}\r\n\r\npublic void TestMethod()\r\n{\r\n    RunWithElevatedPrivileges(SPContext.Current.Web, ()=>\r\n        {\r\n            \/\/ elevated code...\r\n        });\r\n}\r\n<\/pre>\n<p>This way the code will only be encapsulated in the elevated block when the user does not have adequate permissions and you&#8217;ll help to increase the performance of your code by avoiding executing unnecessary code.<\/p>\n<p>You could further extend this by only opening the SPSite\\SPWeb object when elevating the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SharePoint Short #20 Instead of explicitly wrapping code in a RunWithElevatedPrivileges block, it&#8217;s better to first check if the user has sufficient privileges and only elevate when they don&#8217;t. Instead of: SPSecurity.RunWithElevatedPrivileges(() => { \/\/ elevated code&#8230; }); Do this: &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[3,44],"tags":[45],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-qx","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1645"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1645"}],"version-history":[{"count":2,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1645\/revisions"}],"predecessor-version":[{"id":1647,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1645\/revisions\/1647"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}