{"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":"http:\/\/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=\"http:\/\/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":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[3,44],"tags":[45],"class_list":["post-1645","post","type-post","status-publish","format-standard","hentry","category-sharepoint","category-shorts","tag-sharepoint-short"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.3 - aioseo.com -->\n\t<meta name=\"description\" content=\"How to avoid executing SPSecurity.RunWithElevatedPrivileges for all SharePoint users\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Stu\"\/>\n\t<meta name=\"keywords\" content=\"sharepoint,sps,sp,runwithelevatedprivileges,shorts\" \/>\n\t<link rel=\"canonical\" href=\"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.3\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_GB\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Stuart Roberts on all things SharePoint |\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint\" \/>\n\t\t<meta property=\"og:description\" content=\"How to avoid executing SPSecurity.RunWithElevatedPrivileges for all SharePoint users\" \/>\n\t\t<meta property=\"og:url\" content=\"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2014-02-25T15:51:22+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2014-02-25T15:51:22+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@SharePointStu\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint\" \/>\n\t\t<meta name=\"twitter:description\" content=\"How to avoid executing SPSecurity.RunWithElevatedPrivileges for all SharePoint users\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@SharePointStu\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#article\",\"name\":\"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint\",\"headline\":\"Run With Elevated Privilges\",\"author\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/503cb376bab10c66a6bde71d1514dcab2eab4ec44a2f82ef597ca87af61bf1cd?s=96&d=retro&r=g\",\"width\":96,\"height\":96,\"caption\":\"Stu\"},\"datePublished\":\"2014-02-25T15:51:22+00:00\",\"dateModified\":\"2014-02-25T15:51:22+00:00\",\"inLanguage\":\"en-GB\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#webpage\"},\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#webpage\"},\"articleSection\":\"SharePoint, Shorts, SharePoint Short\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\\\/\\\/www.stuartroberts.net\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/category\\\/sharepoint\\\/#listItem\",\"name\":\"SharePoint\"}},{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/category\\\/sharepoint\\\/#listItem\",\"position\":2,\"name\":\"SharePoint\",\"item\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/category\\\/sharepoint\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#listItem\",\"name\":\"Run With Elevated Privilges\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#listItem\",\"position\":3,\"name\":\"Run With Elevated Privilges\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/category\\\/sharepoint\\\/#listItem\",\"name\":\"SharePoint\"}}]},{\"@type\":\"Person\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/#person\",\"name\":\"Stu\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/503cb376bab10c66a6bde71d1514dcab2eab4ec44a2f82ef597ca87af61bf1cd?s=96&d=retro&r=g\",\"width\":96,\"height\":96,\"caption\":\"Stu\"}},{\"@type\":\"Person\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/author\\\/admin\\\/#author\",\"url\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/author\\\/admin\\\/\",\"name\":\"Stu\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/503cb376bab10c66a6bde71d1514dcab2eab4ec44a2f82ef597ca87af61bf1cd?s=96&d=retro&r=g\",\"width\":96,\"height\":96,\"caption\":\"Stu\"}},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#webpage\",\"url\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/\",\"name\":\"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint\",\"description\":\"How to avoid executing SPSecurity.RunWithElevatedPrivileges for all SharePoint users\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/#website\"},\"breadcrumb\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/2014\\\/02\\\/25\\\/quick-tip-20\\\/#breadcrumblist\"},\"author\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/index.php\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2014-02-25T15:51:22+00:00\",\"dateModified\":\"2014-02-25T15:51:22+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/#website\",\"url\":\"http:\\\/\\\/www.stuartroberts.net\\\/\",\"name\":\"Stuart Roberts on all things SharePoint\",\"inLanguage\":\"en-GB\",\"publisher\":{\"@id\":\"http:\\\/\\\/www.stuartroberts.net\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint","description":"How to avoid executing SPSecurity.RunWithElevatedPrivileges for all SharePoint users","canonical_url":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/","robots":"max-image-preview:large","keywords":"sharepoint,sps,sp,runwithelevatedprivileges,shorts","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#article","name":"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint","headline":"Run With Elevated Privilges","author":{"@id":"http:\/\/www.stuartroberts.net\/index.php\/author\/admin\/#author"},"publisher":{"@id":"http:\/\/www.stuartroberts.net\/#person"},"image":{"@type":"ImageObject","@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/503cb376bab10c66a6bde71d1514dcab2eab4ec44a2f82ef597ca87af61bf1cd?s=96&d=retro&r=g","width":96,"height":96,"caption":"Stu"},"datePublished":"2014-02-25T15:51:22+00:00","dateModified":"2014-02-25T15:51:22+00:00","inLanguage":"en-GB","mainEntityOfPage":{"@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#webpage"},"isPartOf":{"@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#webpage"},"articleSection":"SharePoint, Shorts, SharePoint Short"},{"@type":"BreadcrumbList","@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"http:\/\/www.stuartroberts.net\/#listItem","position":1,"name":"Home","item":"http:\/\/www.stuartroberts.net\/","nextItem":{"@type":"ListItem","@id":"http:\/\/www.stuartroberts.net\/index.php\/category\/sharepoint\/#listItem","name":"SharePoint"}},{"@type":"ListItem","@id":"http:\/\/www.stuartroberts.net\/index.php\/category\/sharepoint\/#listItem","position":2,"name":"SharePoint","item":"http:\/\/www.stuartroberts.net\/index.php\/category\/sharepoint\/","nextItem":{"@type":"ListItem","@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#listItem","name":"Run With Elevated Privilges"},"previousItem":{"@type":"ListItem","@id":"http:\/\/www.stuartroberts.net\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#listItem","position":3,"name":"Run With Elevated Privilges","previousItem":{"@type":"ListItem","@id":"http:\/\/www.stuartroberts.net\/index.php\/category\/sharepoint\/#listItem","name":"SharePoint"}}]},{"@type":"Person","@id":"http:\/\/www.stuartroberts.net\/#person","name":"Stu","image":{"@type":"ImageObject","@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/503cb376bab10c66a6bde71d1514dcab2eab4ec44a2f82ef597ca87af61bf1cd?s=96&d=retro&r=g","width":96,"height":96,"caption":"Stu"}},{"@type":"Person","@id":"http:\/\/www.stuartroberts.net\/index.php\/author\/admin\/#author","url":"http:\/\/www.stuartroberts.net\/index.php\/author\/admin\/","name":"Stu","image":{"@type":"ImageObject","@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/503cb376bab10c66a6bde71d1514dcab2eab4ec44a2f82ef597ca87af61bf1cd?s=96&d=retro&r=g","width":96,"height":96,"caption":"Stu"}},{"@type":"WebPage","@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#webpage","url":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/","name":"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint","description":"How to avoid executing SPSecurity.RunWithElevatedPrivileges for all SharePoint users","inLanguage":"en-GB","isPartOf":{"@id":"http:\/\/www.stuartroberts.net\/#website"},"breadcrumb":{"@id":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/#breadcrumblist"},"author":{"@id":"http:\/\/www.stuartroberts.net\/index.php\/author\/admin\/#author"},"creator":{"@id":"http:\/\/www.stuartroberts.net\/index.php\/author\/admin\/#author"},"datePublished":"2014-02-25T15:51:22+00:00","dateModified":"2014-02-25T15:51:22+00:00"},{"@type":"WebSite","@id":"http:\/\/www.stuartroberts.net\/#website","url":"http:\/\/www.stuartroberts.net\/","name":"Stuart Roberts on all things SharePoint","inLanguage":"en-GB","publisher":{"@id":"http:\/\/www.stuartroberts.net\/#person"}}]},"og:locale":"en_GB","og:site_name":"Stuart Roberts on all things SharePoint |","og:type":"article","og:title":"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint","og:description":"How to avoid executing SPSecurity.RunWithElevatedPrivileges for all SharePoint users","og:url":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/","article:published_time":"2014-02-25T15:51:22+00:00","article:modified_time":"2014-02-25T15:51:22+00:00","twitter:card":"summary","twitter:site":"@SharePointStu","twitter:title":"Quick Tip #20 - Run With Elevated Privilges | Stuart Roberts on all things SharePoint","twitter:description":"How to avoid executing SPSecurity.RunWithElevatedPrivileges for all SharePoint users","twitter:creator":"@SharePointStu"},"aioseo_meta_data":{"post_id":"1645","title":"Quick Tip #20 - Run With Elevated Privilges | #site_title","description":"How to avoid executing SPSecurity.RunWithElevatedPrivileges  for all SharePoint users","keywords":[{"label":"SharePoint","value":"SharePoint"},{"label":"SPS","value":"SPS"},{"label":"SP","value":"SP"},{"label":"RunWithElevatedPrivileges","value":"RunWithElevatedPrivileges"}],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-08-03 13:46:15","updated":"2026-10-16 15:50:56","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"http:\/\/www.stuartroberts.net\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"http:\/\/www.stuartroberts.net\/index.php\/category\/sharepoint\/\" title=\"SharePoint\">SharePoint<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tRun With Elevated Privilges\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"http:\/\/www.stuartroberts.net\/"},{"label":"SharePoint","link":"http:\/\/www.stuartroberts.net\/index.php\/category\/sharepoint\/"},{"label":"Run With Elevated Privilges","link":"http:\/\/www.stuartroberts.net\/index.php\/2014\/02\/25\/quick-tip-20\/"}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1645"}],"version-history":[{"count":2,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1645\/revisions"}],"predecessor-version":[{"id":1647,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1645\/revisions\/1647"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1645"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}