<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Just Another Web Application : elevated</title><link>http://blogs.technet.com/jasbro/archive/tags/elevated/default.aspx</link><description>Tags: elevated</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>SPSecurity.RunWithElevatedPrivileges()</title><link>http://blogs.technet.com/jasbro/archive/2007/12/12/spsecurity-runwithelevatedprivileges.aspx</link><pubDate>Wed, 12 Dec 2007 03:09:00 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:2636833</guid><dc:creator>jasbro</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.technet.com/jasbro/comments/2636833.aspx</comments><wfw:commentRss>http://blogs.technet.com/jasbro/commentrss.aspx?PostID=2636833</wfw:commentRss><description>&lt;P&gt;The subject of today's post: running code in sharepoint with elevated rights, an operation sometimes required, sometimes abused and often misunderstood.&lt;/P&gt;
&lt;P&gt;The WSS Object Model provides a huge number of classes, some of which can carry-out potentially dodgy actions, so require elevation to run. Ordinarily you'd just deal with this by logging in as a user with rights to carry out the operation, but occasionally this isn't practical or possible, and that's where today's subject comes in.&lt;/P&gt;
&lt;P&gt;Let's say, just as an example, you're creating an anonymously-accessible site. In a Control on on of the pages you want to enumerate subsites of your site, and grab some properties thereof, maybe for display, maybe for some other operation in your code - however, this isn't something an anonymous identity can do.&lt;/P&gt;
&lt;P&gt;In steps our hero - RunWithElevatedPrivileges()&lt;/P&gt;
&lt;P&gt;Used correctly, this method allows a specified block of code to run in the context of the SharePoint System Account, a powerful method with much potential. Here's the summary &lt;A class="" title="SDK Reference to SPSecurity.RunWithElevatedPrivileges()" href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx"&gt;from the SDK&lt;/A&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;CODE&gt;
&lt;DIV class=CodeDisplayLanguage&gt;[SharePointPermissionAttribute(SecurityAction.Demand, Impersonate=&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)] &lt;BR&gt;[SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel=&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)] &lt;BR&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; RunWithElevatedPrivileges (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeToRunElevated secureCode&lt;BR&gt;)&lt;/DIV&gt;&lt;/CODE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;Now the CodeToRunElevated parameter can be a reference to a void, parameterless method or an anonymous method via&amp;nbsp;delegate() - please, follow the SDK link if that's unclear.&lt;/P&gt;
&lt;P&gt;Pretty simple, huh? Yep, well as always there's a catch or two.&lt;/P&gt;
&lt;P&gt;1. If you're manipulating any Object Model elements within your elevated method, you need to get a fresh&amp;nbsp;SPSite reference inside this call. For example&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;CODE&gt;SPSecurity.RunWithElevatedPrivileges(delegate(){&lt;BR&gt;&amp;nbsp;&amp;nbsp; SPSite mySite = new SPSite(&lt;A href="http://sharepoint/"&gt;http://sharepoint/&lt;/A&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp; SPWeb myWeb = SPSite.OpenWeb();&lt;BR&gt;&amp;nbsp;&amp;nbsp; // further implementation omitted&lt;BR&gt;});&lt;/CODE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;2. You can't just use &lt;CODE&gt;SPContext.Current.Site&lt;/CODE&gt; to get your SPSite reference - or you'll ber handed the object with the security context of the anonymous (or non-elevated)&amp;nbsp;user and &lt;EM&gt;your elevation will not work as expected&lt;/EM&gt;.&lt;/P&gt;
&lt;P mce_keep="true"&gt;3. If you need to Update() anything inside this block, you'll need to call SPSite.AllowUnsafeUpdates() on your new site reference (or web reference) &lt;A class="" title="AllowUnsafeUpdates in the SDK" href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.allowunsafeupdates.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.allowunsafeupdates.aspx"&gt;as per this SDK entry&lt;/A&gt;.&lt;/P&gt;
&lt;P mce_keep="true"&gt;So those are the gotchas. Following those we have the obvious security warnings - be careful what you do within this call, as the system identity has full control over SharePoint and could do Very Bad Things if incorrectly used. Sanitise any user input very carefully if you're going to let it anywhere near this method - you certainly&amp;nbsp;don't want a user finding some injectable exploit into this code. Exercise caution over what you do, for this power must be used wisely. But you knew that, right?&lt;/P&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=2636833" width="1" height="1"&gt;</description><category domain="http://blogs.technet.com/jasbro/archive/tags/sharepoint/default.aspx">sharepoint</category><category domain="http://blogs.technet.com/jasbro/archive/tags/sdk/default.aspx">sdk</category><category domain="http://blogs.technet.com/jasbro/archive/tags/security/default.aspx">security</category><category domain="http://blogs.technet.com/jasbro/archive/tags/elevated/default.aspx">elevated</category></item></channel></rss>