<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nicomputer&#039;s blog &#187; .net</title>
	<atom:link href="http://blog.nicomputer.com/articles/programming/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nicomputer.com</link>
	<description>I am (not?) a Robot!</description>
	<lastBuildDate>Tue, 08 Dec 2009 13:53:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>SharePoint, Execution Context &amp; Security</title>
		<link>http://blog.nicomputer.com/programming/net/sharepoint-execution-context-security/</link>
		<comments>http://blog.nicomputer.com/programming/net/sharepoint-execution-context-security/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 12:07:13 +0000</pubDate>
		<dc:creator>--Nico</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://blog.nicomputer.com/?p=181</guid>
		<description><![CDATA[<em>(I have originally published this article on the <a href="">Orckestra's Dot Net For Thougts Blog</a>)</em>

At least once in his career, a SharePoint developer is going to be faced with a security problem.

When a user executes an action in SharePoint, i.e. modifies an item in a list, the request on the server executes using the security context of the user. So, if the user does not have the right privileges to accomplish specific actions, the request could fail with a security exception.

A way to overcome this behaviour is to use the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx">RunWithElevatedPrivileges method from the SPUtility class</a>.]]></description>
			<content:encoded><![CDATA[<p><em>(I have originally published this article on the <a href="http://www.dotnetforthoughts.com/post/2009/11/17/SharePoint-Execution-Context-Security.aspx">Orckestra&#8217;s Dot Net For Thougts Blog</a>)</em></p>
<p>At least once in his career, a SharePoint developer is going to be faced with a security problem.</p>
<p>When a user executes an action in SharePoint, i.e. modifies an item in a list, the request on the server executes using the security context of the user. So, if the user does not have the right privileges to accomplish specific actions, the request could fail with a security exception.</p>
<p>A way to overcome this behaviour is to use the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx">RunWithElevatedPrivileges method from the SPUtility class</a>.</p>
<p><span id="more-181"></span></p>
<p>An important thing to keep in mind when using this method is the initial context of the object on which you will execute specific actions.</p>
<p>In the example below, even if you use the RunWithElevatedPrivileges method, the security context of the SPWeb object (<em>web</em> variable) is inherited from the user permissions.<br />
This behaviour is normal because the SPSite <em>site</em> variable is a reference to the <em>properties</em> variable which was created with the user’s security context.</p>
<pre class="brush: csharp; title: ;">
public override void ItemUpdated(SPItemEventProperties properties)
{
	SPSecurity.RunWithElevatedPrivileges(delegate(){
		using (SPSite site = properties.ListItem.Web.Site)
		{
			using (SPWeb web = site.RootWeb)
			{
				web.AllowUnsafeUpdates = true;
                                // Do some actions
			}
		}
	});
}
</pre>
<p>To run in a context with full control, all objects <strong>should be instantiated</strong> inside the RunWithElevatedPrivileges delegate method.<br />
By doing this, objects will inherit their permission from the RunWithElevatedPrivileges context which is full control:</p>
<pre class="brush: csharp; title: ;">
public override void ItemUpdated(SPItemEventProperties properties)
{
	SPSecurity.RunWithElevatedPrivileges(delegate(){
		using (SPSite site = new SPSite(properties.ListItem.Web.Site.ID))
		{
			using (SPWeb web = site.RootWeb)
			{
				web.AllowUnsafeUpdates = true;
                                // Do some actions
			}
		}
	});
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.nicomputer.com/programming/net/sharepoint-execution-context-security/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

