Exception encountered using SPSecurity.RunWithElevatedPrivileges<p>I am encountering an exception when attempting to run elevated code within a web part, and not obtaining an exception when the same code is run from within a list item event handler.</p> <p>In both cases, I am performing the following steps:</p> <p>1) Elevating to the SHAREPOINT\system account using SPSecurity.RunWithElevatedPrivileges</p> <p>2) Opening a new SPSite using the site GUID of the list item I am going to modify, new SPSite(item.Web.Site.ID)</p> <p>3) Opening a new SPWeb using the web GUID of the list item I am going to modify, Site.OpenWeb(item.Web.ID)</p> <p>4) Opening a new instance of the list item, web.Lists[item.ParentList.ID].GetItemById(item.ID)</p> <p>5) Breaking the security role inheritance of the new list item, newItem.BreakRoleInheritance</p> <p>6) Updating the new list item, newItem.Update</p> <p>When I perform the above steps from within a web part, I recieve the following exception: Microsoft.SharePoint.SPException: The security validation for this page is invalid.</p> <p>When I perform the above steps from within an ItemUpdated event handler, I do not recieve an exception, and the requested operations are performed correctly.</p> <p>Utilizing an event handler to call this code poses a serious issue.  I must periodically update the permissions of the list item based on the value of several of the fields.  Updating the role assignments for the item inside the ItemUpdated generates subsequent event-handler calls--which will eventually terminate in a save conflict exception.</p> <p>Are there extra steps that I need to take when inside web part-based code to run elevated code as described above?</p> <p>Thank you.</p>© 2009 Microsoft Corporation. All rights reserved.Thu, 09 Apr 2009 02:01:10 Z63106621-cbde-44d9-b3cf-07bddc0856f1http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#63106621-cbde-44d9-b3cf-07bddc0856f1http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#63106621-cbde-44d9-b3cf-07bddc0856f1jrhoffhttp://social.msdn.microsoft.com/Profile/en-US/?user=jrhoffException encountered using SPSecurity.RunWithElevatedPrivileges<p>I am encountering an exception when attempting to run elevated code within a web part, and not obtaining an exception when the same code is run from within a list item event handler.</p> <p>In both cases, I am performing the following steps:</p> <p>1) Elevating to the SHAREPOINT\system account using SPSecurity.RunWithElevatedPrivileges</p> <p>2) Opening a new SPSite using the site GUID of the list item I am going to modify, new SPSite(item.Web.Site.ID)</p> <p>3) Opening a new SPWeb using the web GUID of the list item I am going to modify, Site.OpenWeb(item.Web.ID)</p> <p>4) Opening a new instance of the list item, web.Lists[item.ParentList.ID].GetItemById(item.ID)</p> <p>5) Breaking the security role inheritance of the new list item, newItem.BreakRoleInheritance</p> <p>6) Updating the new list item, newItem.Update</p> <p>When I perform the above steps from within a web part, I recieve the following exception: Microsoft.SharePoint.SPException: The security validation for this page is invalid.</p> <p>When I perform the above steps from within an ItemUpdated event handler, I do not recieve an exception, and the requested operations are performed correctly.</p> <p>Utilizing an event handler to call this code poses a serious issue.  I must periodically update the permissions of the list item based on the value of several of the fields.  Updating the role assignments for the item inside the ItemUpdated generates subsequent event-handler calls--which will eventually terminate in a save conflict exception.</p> <p>Are there extra steps that I need to take when inside web part-based code to run elevated code as described above?</p> <p>Thank you.</p>Thu, 18 Jan 2007 23:30:59 Z2008-03-01T22:06:20Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#c152373d-f6ee-406e-ba76-57e656d11a48http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#c152373d-f6ee-406e-ba76-57e656d11a48AndersRhttp://social.msdn.microsoft.com/Profile/en-US/?user=AndersRException encountered using SPSecurity.RunWithElevatedPrivileges<p>About the security validation: since i havent seen your code it might be a shot in the dark, but try and enable AllowUnsafeUpdates on the site (SPWeb) you are accessing:</p> <p>myWeb.AllowUnsafeUpdates = true;</p> <p> </p> <p>Regarding recursive event firing you can disable event firing while updating list item:</p> <p>this.DisableEventFiring();<br><br>// do changes here</p> <p>// when changes is done use SystemUpdate(false);<br>// SystemUpdate wont change modified or modifiedBy fields</p> <p>yourListItem.SystemUpdate(false);</p> <p>this.EnableEventFiring();</p> <p> </p> <p>Additional reading:</p> <p><a title="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.speventreceiverbase_methods.aspx" href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.speventreceiverbase_methods.aspx">http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.speventreceiverbase_methods.aspx</a></p> <p> </p> <p>hth</p> <p> </p>Sat, 20 Jan 2007 09:41:27 Z2007-01-20T09:41:27Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#01623f32-57b1-4580-8587-dae3b59a655chttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#01623f32-57b1-4580-8587-dae3b59a655cPaul Yauhttp://social.msdn.microsoft.com/Profile/en-US/?user=Paul%20YauException encountered using SPSecurity.RunWithElevatedPrivileges<p>i have the same requirementas you do. I need to change the item's permission based on the value of fields (e.g. login name)</p> <p> </p> <p>but i got the following error :<font size=1></p> <p>: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))</p> <p> </p> <p> </p></font>Sun, 01 Apr 2007 14:22:30 Z2007-04-01T14:22:30Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#afb8da86-8d0f-4965-bd83-e4b2e9bdddaahttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#afb8da86-8d0f-4965-bd83-e4b2e9bdddaaKen Morencyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Ken%20MorencyException encountered using SPSecurity.RunWithElevatedPrivilegesTough to say what's causing the issue. You could try setting the CatchAccessDeniedException to False on the SPSite object.Mon, 02 Apr 2007 22:48:45 Z2007-04-02T22:48:45Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#5638b8f6-1699-42ff-9258-642d9e034647http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#5638b8f6-1699-42ff-9258-642d9e034647Paul Yauhttp://social.msdn.microsoft.com/Profile/en-US/?user=Paul%20YauException encountered using SPSecurity.RunWithElevatedPrivileges<p> </p> <p>I guess the problem is captured in the MS KB:</p> <p class=list4 style="margin:7.5pt 3.75pt"><span style="font-size:8.5pt;color:black;font-family:'Verdana','sans-serif'"><a title="http://support.microsoft.com/kb/932056/" href="http://support.microsoft.com/kb/932056/"><u><font color="#0000ff">932056</font></u></a></span><span class=plink1><span style="font-size:7pt;color:black;font-family:'Verdana','sans-serif'"> (</span></span><span class=plink1><span style="font-size:7pt;color:black;font-family:'Verdana','sans-serif'"><span style=""><span class=MsoHyperlink><u><font color="#0000ff">http://support.microsoft.com/kb/932056/</font></u></span></span></span></span><span class=plink1><span style="font-size:7pt;color:black;font-family:'Verdana','sans-serif'">)</span></span><span style="font-size:8.5pt;color:black;font-family:'Verdana','sans-serif'"> One or more custom programs do not finish successfully when you run multiple custom programs that use the BreakRoleInheritance function in the Windows SharePoint Services 3.0 object model </span></p> <p> </p> <p>When i debug the program, run to BreakRoleInheritance function, it catches an exception and the function was not exectued. So the permission of the list item in Document Library cannot be updated.</p> <p> </p> <p>Any one gets the same problem?</p>Tue, 03 Apr 2007 04:03:49 Z2007-04-03T04:03:49Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#86b5e655-e74f-4860-bf7d-68cff9f28ba8http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#86b5e655-e74f-4860-bf7d-68cff9f28ba8a1ginhttp://social.msdn.microsoft.com/Profile/en-US/?user=a1ginException encountered using SPSecurity.RunWithElevatedPrivileges<p>hi,</p> <p>Has anybody solved the problem with the <font color="#008000" size=2>BreakRoleInheritance <font color="#000000" size=3>invoking? </font></p></font>Fri, 06 Apr 2007 12:34:29 Z2007-04-06T12:34:29Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#17ca72b9-263e-4096-b39e-bf236754f1c4http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#17ca72b9-263e-4096-b39e-bf236754f1c4Wouterhttp://social.msdn.microsoft.com/Profile/en-US/?user=WouterException encountered using SPSecurity.RunWithElevatedPrivileges<p>Hi,</p> <p> </p> <p>Same problem here, is there a solution/workaround?</p>Fri, 20 Apr 2007 08:07:33 Z2007-04-20T08:07:33Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#981a35b4-d7f2-4ef4-b715-af9c689f916bhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#981a35b4-d7f2-4ef4-b715-af9c689f916beddy2705http://social.msdn.microsoft.com/Profile/en-US/?user=eddy2705Exception encountered using SPSecurity.RunWithElevatedPrivileges<p>have you tried impersonation (as the current user)?</p><font size=1><font size=1> <p></font><font color="#0000ff" size=1></font> </p><font color="#0000ff" size=1><b><font size=1> <p></b></font><font color="#0000ff" size=1>string</font><b><font size=1> </b></font><font color="#800000" size=1>portalName</font><b><font size=1>;</p> <p></b></font><font color="#800000" size=1>SPSite</font><b><font size=1> </b></font><font color="#800000" size=1>portal</font><b><font size=1> = </b></font><font color="#800000" size=1>SPControl</font><font size=1>.</font><font color="#800000" size=1>GetContextSite</font><b><font size=1>(</b></font><font color="#0000ff" size=1>this</font><font size=1>.</font><font color="#800000" size=1>Context</font><b><font size=1>);</p> <p></b></font><font color="#800000" size=1>portalName</font><b><font size=1> = &quot;http://&quot; + </b></font><font color="#800000" size=1>portal</font><font size=1>.</font><font color="#800000" size=1>HostName</font><b><font size=1> + &quot;:&quot; + </b></font><font color="#800000" size=1>portal</font><font size=1>.</font><font color="#800000" size=1>Port</font><font size=1>.</font><font color="#800000" size=1>ToString</font><b><font size=1>();</font></b></p> <p><b><font size=1></font></b> </p> <p><b><font size=1> </p></b></font><font size=1><b> <p></b></font><font color="#800000" size=1>System</font><font size=1>.</font><font color="#800000" size=1>Security</font><font size=1>.</font><font color="#800000" size=1>Principal</font><font size=1>.</font><font color="#800000" size=1>WindowsImpersonationContext</font><b><font size=1> </b></font><font color="#800000" size=1>wic</font><b><font size=1> = </b></font><font color="#0000ff" size=1>null</font><b><font size=1>;</p> <p></b></font><font color="#800000" size=1>wic</font><b><font size=1> = </b></font><font color="#800000" size=1>System</font><font size=1>.</font><font color="#800000" size=1>Security</font><font size=1>.</font><font color="#800000" size=1>Principal</font><font size=1>.</font><font color="#800000" size=1>WindowsIdentity</font><font size=1>.</font><font color="#800000" size=1>GetCurrent</font><b><font size=1>().</b></font><font color="#800000" size=1>Impersonate</font><b><font size=1>();</p></b><b> <p></b></font><font color="#0000ff" size=1>using</font><b><font size=1> (</b></font><font color="#800000" size=1>SPSite</font><b><font size=1> </b></font><font color="#800000" size=1>site</font><b><font size=1> = </b></font><font color="#0000ff" size=1>new</font><b><font size=1> </b></font><font color="#800000" size=1>SPSite</font><b><font size=1>(</b></font><font color="#800000" size=1>portalName</font><b><font size=1>))</p> <p>{ </p></b></font><b><font size=1> <blockquote dir=ltr style="margin-right:0px"> <p></b></font><font color="#0000ff" size=1>string</font><b><font size=1> </b></font><font color="#800000" size=1>refSiteName</font><b><font size=1> = </b></font><font color="#800000" size=1>System</font><font size=1>.</font><font color="#800000" size=1>Configuration</font><font size=1>.</font><font color="#800000" size=1>ConfigurationSettings</font><font size=1>.</font><font color="#800000" size=1>AppSettings</font><b><font size=1>[&quot;RefSite&quot;].</b></font><font color="#800000" size=1>ToString</font><b><font size=1>();</p></b></font><font size=1><b> <p></b></font><font color="#0000ff" size=1>using</font><b><font size=1> (</b></font><font color="#800000" size=1>SPWeb</font><b><font size=1> </b></font><font color="#800000" size=1>refWeb</font><b><font size=1> = </b></font><font color="#800000" size=1>site</font><font size=1>.</font><font color="#800000" size=1>OpenWeb</font><b><font size=1>(</b></font><font color="#800000" size=1>refSiteName</font><b><font size=1>))</p> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></b></font><font color="#0000ff" size=1>try</p></font><b><font size=1> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></b></font><font color="#800000" size=1>refWeb</font><font size=1>.</font><font color="#800000" size=1>AllowUnsafeUpdates</font><b><font size=1> = </b></font><font color="#0000ff" size=1>true</font><b><font size=1>;</font></b></p><b><font size=1><font color="#008000" size=1> <p>//do your thing</p></font></blockquote></b></font><b><font size=1> <p>}</p> <p></b></font><font color="#0000ff" size=1>catch</font><b><font size=1> (</b></font><font color="#800000" size=1>Exception</font><b><font size=1> </b></font><font color="#800000" size=1>e</font><b><font size=1>)</p> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></b></font><font color="#800000" size=1>log</font><font size=1>.</font><font color="#800000" size=1>Error</font><b><font size=1>(</b></font><font color="#800000" size=1>e</font><font size=1>.</font><font color="#800000" size=1>Message</font><b><font size=1> + &quot;\n&quot; + </b></font><font color="#800000" size=1>e</font><font size=1>.</font><font color="#800000" size=1>StackTrace</font><b><font size=1>);</p></blockquote> <p>}</p></blockquote> <p>}</p> <p></b></font><font color="#800000" size=1>wic</font><font size=1>.</font><font color="#800000" size=1>Undo</font><b><font size=1>();</p></blockquote> <p>}</p></b></font></font></font>Fri, 20 Apr 2007 13:27:27 Z2007-04-20T13:27:27Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#a938b342-3295-4d61-8253-be48d20dd8eahttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#a938b342-3295-4d61-8253-be48d20dd8eaPaul Yauhttp://social.msdn.microsoft.com/Profile/en-US/?user=Paul%20YauException encountered using SPSecurity.RunWithElevatedPrivileges<p>Hi everyone, any update on this issue?</p> <p> </p> <p>I have the same exception.</p> <p> </p><font size=2> <p>Impersonator im = </font><font color="#0000ff" size=2>new</font><font size=2> Impersonator(_mossAdminName, _domain, _mossAdminPwd);</p> <p></p> <p></font><font color="#0000ff" size=2>try</p></font><font size=2> <p>{</p> <p>im.Impersonate();</p> <p></font><font color="#008000" size=2> </p></font><font size=2> <p></font><font color="#0000ff" size=2>try</p></font><font size=2> <p>{</p> <p>web.AllowUnsafeUpdates = </font><font color="#0000ff" size=2>true</font><font size=2>;</p> <p>item.BreakRoleInheritance(</font><font color="#0000ff" size=2>false</font><font size=2>); //item is a SPListItem or SPList</p> <p>}</p> <p></font><font color="#0000ff" size=2>catch</font><font size=2> </p> <p>{</p> <p>}</p> <p> </p> <p>}catch{}finally{im.undo();}</p> <p></font><font size=2> </p></font>Fri, 08 Jun 2007 06:15:03 Z2007-06-08T06:15:03Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#7079a306-a092-4bd6-8289-b85b0f67edd1http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#7079a306-a092-4bd6-8289-b85b0f67edd1Paul Yauhttp://social.msdn.microsoft.com/Profile/en-US/?user=Paul%20YauException encountered using SPSecurity.RunWithElevatedPrivileges<p>I only received this error in breaking document library and folder permission.</p> <p> </p> <p>[date time] [Error]Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT)) </p> <p> </p> <p>It's fine when breaking file's permission inheritance. Any idea?</p>Fri, 08 Jun 2007 06:28:03 Z2007-06-08T06:28:03Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#2b29488f-88fd-4d73-8d9d-97d7e400b5a8http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#2b29488f-88fd-4d73-8d9d-97d7e400b5a8cwoglehttp://social.msdn.microsoft.com/Profile/en-US/?user=cwogleException encountered using SPSecurity.RunWithElevatedPrivileges<p>I encountered two problems while trying to break the permission inheritance.</p> <p align=left> </p> <p align=left>1. BreakRoleInheritance(false) throws an exception. I can find no solution, so my workaround is to call BreakRoleInheritance(true) and programmatically remove all permissions.</p> <p align=left> </p> <p align=left>2. BreakRoleInheritance() sets AllowUnsafeUpdates to false. I set it back to true again.</p> <p align=left> </p> <p align=left>Here's my working code:</p> <p align=left> </p><font size=2> <p align=left></font><font color="#008000" size=2> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Block</span></div> <p align=left><font color="#008000" size=2>// Run as system administrator.</p></font><font size=2> <p align=left></font><font color="#008080" size=2>SPSecurity</font><font size=2>.RunWithElevatedPrivileges(</font><font color="#0000ff" size=2>delegate</font><font size=2>()</p> <p align=left>{</p> <p align=left></font><font color="#008000" size=2>// Get current site collection.</p></font><font size=2> <p align=left></font><font color="#0000ff" size=2>using</font><font size=2> (</font><font color="#008080" size=2>SPSite</font><font size=2> SiteCollection = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>SPSite</font><font size=2>(</font><font color="#008080" size=2>SPControl</font><font size=2>.GetContextSite(System.Web.</font><font color="#008080" size=2>HttpContext</font><font size=2>.Current).ID))</p> <p align=left>{</p> <p align=left>SiteCollection.AllowUnsafeUpdates = </font><font color="#0000ff" size=2>true</font><font size=2>; // Not sure this is necessary</p></font><font size=2> <p align=left></font><font color="#008080" size=2>SPWeb</font><font size=2> myWeb = SiteCollection.OpenWeb(...);</p> <p align=left>myWeb.AllowUnsafeUpdates = </font><font color="#0000ff" size=2>true</font><font size=2>;</p></font><font size=2> <p align=left></font><font color="#008080" size=2>SPList</font><font size=2> myList = myWeb.Lists[&quot;ListName&quot;];</p> <p align=left></font><font color="#008000" size=2>// Remove all permissions.</p></font><font size=2> <p align=left></font><font color="#0000ff" size=2>if</font><font size=2> (!myList.HasUniqueRoleAssignments)</p> <p align=left>{</p> <p align=left></font><font color="#008000" size=2>// There's a bug in BreakRoleInheritance(). Doesn't work when you pass</p></font><font size=2> <p align=left></font><font color="#008000" size=2>// 'false', so pass 'true' and manually delete all permissions.</p></font><font size=2> <p align=left>myList.BreakRoleInheritance(</font><font color="#0000ff" size=2>true</font><font size=2>);</p> <p align=left></font><font color="#008000" size=2>// BreakRoleInheritance sets myWeb.AllowUnsafeUpdates back to false,</p></font><font size=2> <p align=left></font><font color="#008000" size=2>// so put it back to true;</p></font><font size=2> <p align=left>myWeb.AllowUnsafeUpdates = </font><font color="#0000ff" size=2>true</font><font size=2>;</p> <p align=left>}</p> <p align=left></font><font color="#0000ff" size=2>while</font><font size=2> (myList.RoleAssignments.Count &gt; 0)</p> <p align=left>{</p> <p align=left>myList.RoleAssignments.Remove(0);</p> <p align=left>}</p> <p align=left>myList.Update();</p> <p align=left>}</p> <p align=left>});</p></font> <p align=left> </p></div></div> <p align=left> </p> <p></p></font><font size=2></font> <p align=left><font face=Arial size=2></font> </p> <p>hth,</p> <p align=left>Chris</p>Mon, 15 Oct 2007 13:13:55 Z2007-10-15T13:13:55Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#cb539b90-a09a-4d4d-b7ab-9af406c86529http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#cb539b90-a09a-4d4d-b7ab-9af406c86529Paul Yauhttp://social.msdn.microsoft.com/Profile/en-US/?user=Paul%20YauException encountered using SPSecurity.RunWithElevatedPrivileges<font face=Arial size=2> <p class=MsoNormal><b><span style="color:#1f497d">Hi all,</span></b></p> <p class=MsoNormal align=left><b><span style="color:#1f497d"></span></b> </p> <p class=MsoNormal align=left><b><span style="color:#1f497d">My MS support info for everyone.</span></b></p> <p class=MsoNormal align=left><b><span style="color:#1f497d"></span></b> </p> <p class=MsoNormal align=left><b><span style="color:#1f497d">Problem Description:</span></b></p> <p class=MsoNormal><span style="color:#1f497d">===============</span></p> <p class=MsoNormal><span style="color:#1f497d">When you call SPList.BreakRoleInheritance(false) from an HTTP GET request, although you have specified SPWeb.AllUnsafeUpdates=true, you will still be thrown an exception</span></p> <p class=MsoNormal><span style="color:red">Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.</span></p> <p class=MsoNormal><span style="color:#1f497d"></span> </p> <p class=MsoNormal><b><span style="color:#1f497d">Cause:</span></b></p> <p class=MsoNormal><span style="color:#1f497d">=====</span></p> <p class=MsoNormal><span style="color:#1f497d">This is by design limitation of SPList.BreakRoleInheritance</span></p> <p class=MsoNormal><span style="color:#1f497d"></span> </p> <p class=MsoNormal><span style="color:#1f497d">BreakRoleInheritance does it work in two steps. First, it needs to revert its permission to have same permission settings as parent (this is a less expensive operation, and give the list a fresh start on its road to unique permission). Later it checks CopyRoleAssignments parameter. If it is false, it takes an extra step to clean up permission on the list. A side effect of step 1 is that it dirties some internal objects in SPWeb, and cause them to be recreated. Unfortunately, the re-creation of those internal objects cause SPWeb.AllowUnsafeUpdates to have a default value which is false. That is, SPWeb.AllowUnsafeUpdates is reset in middle of call to SPList.BreakRoleInheritance, therefore we got the exception.</span></p> <p class=MsoNormal><span style="color:#1f497d"></span> </p> <p class=MsoNormal><b><span style="color:#1f497d">Resolution:</span></b></p> <p class=MsoNormal><span style="color:#1f497d">========</span></p> <p class=MsoNormal><span style="color:#1f497d">There are two possible workarounds to the issue:</span></p> <p class=MsoNormal><span style="color:#1f497d"></span> </p> <p class=MsoListParagraph style="margin-left:0.25in;text-indent:-0.25in"><span style="color:#1f497d"><span>1.<span style="font:7pt 'Times New Roman'">       </span></span></span><span style="color:#1f497d">Call SPList.BreakRoleInheritance from a HTTP POST request. That is, we can first have a button on UI and have users to click. In response to users’ click, we call SPList.BreakRoleInheritance. There is a first HTTP GET request by which, SharePoint has a chance to embed some digest to validate requests on return (HTTP POST). Therefore, we no longer need to set SPWeb.AllowUnsafeUpdates=true. This is recommended approach from security perspective.</span></p> <p class=MsoNormal><span style="color:#1f497d"></span> </p> <p class=MsoListParagraph style="margin-left:0.25in;text-indent:-0.25in"><span style="color:#1f497d"><span>2.<span style="font:7pt 'Times New Roman'">       </span></span></span><span style="color:#1f497d">First call SPList.BreakRoleInheritance(true). Then, use custom code to clean up permission and create your own permission set for the list as needed. The sample code are:</span></p> <p class=MsoNormal><span style="color:#1f497d"></span> </p> <p class=MsoNormal><span style="font-size:9pt;color:teal;font-family:新宋体">SPWeb</span><span style="font-size:9pt;font-family:新宋体"> web = <span style="color:teal">SPControl</span>.GetContextWeb(<span style="color:blue">this</span>.Context);</span></p> <p class=MsoNormal><span style="font-size:9pt;color:teal;font-family:新宋体">SPListCollection</span><span style="font-size:9pt;font-family:新宋体"> lists = web.Lists;</span></p> <p class=MsoNormal><span style="font-size:9pt;font-family:新宋体"></span> </p> <p class=MsoNormal><span style="font-size:9pt;color:green;font-family:新宋体">//Guid docLibGuid = lists.Add(&quot;Doc Lib Sample 1&quot;, &quot;Doc Lib Desc&quot;, SPListTemplateType.DocumentLibrary);</span></p> <p class=MsoNormal><span style="font-size:9pt;color:green;font-family:新宋体">//SPList docLib = lists[docLibGuid];</span></p> <p class=MsoNormal><span style="font-size:9pt;color:teal;font-family:新宋体">SPList</span><span style="font-size:9pt;font-family:新宋体"> docLib = lists[<span style="color:maroon">&quot;Doc Lib Sample 1&quot;</span>];</span></p> <p class=MsoNormal><span style="font-size:9pt;color:green;font-family:新宋体">//docLib.ParentWeb.AllowUnsafeUpdates = true;</span></p> <p class=MsoNormal><span style="font-size:9pt;font-family:新宋体">docLib.BreakRoleInheritance(<span style="color:blue">true</span>); <span style="color:green">//Exception throw here when the parameters is &quot;false&quot;</span></span></p> <p class=MsoNormal><span style="font-size:9pt;color:green;font-family:新宋体"></span> </p> <p class=MsoNormal><span style="font-size:9pt;font-family:新宋体">web.AllowUnsafeUpdates = <span style="color:blue">true</span>;</span></p> <p class=MsoNormal><span style="font-size:9pt;color:teal;font-family:新宋体">SPRoleAssignmentCollection</span><span style="font-size:9pt;font-family:新宋体"> roleAssigns = docLib.RoleAssignments;</span></p> <p class=MsoNormal><span style="font-size:9pt;color:blue;font-family:新宋体">for</span><span style="font-size:9pt;font-family:新宋体"> (<span style="color:blue">int</span> i = roleAssigns.Count-1; i &gt;= 0; i--)</span></p> <p class=MsoNormal><span style="font-size:9pt;font-family:新宋体">{</span></p> <p class=MsoNormal><span style="font-size:9pt;font-family:新宋体">    roleAssigns.Remove(i);</span></p> <p class=MsoNormal><span style="font-size:9pt;font-family:新宋体">}</span><span style="color:#1f497d"></span></p></font>Thu, 18 Oct 2007 10:07:00 Z2007-10-18T10:07:00Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#850d9a0d-7834-474c-8602-5f7d61e31fb4http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#850d9a0d-7834-474c-8602-5f7d61e31fb4Saroj Jhahttp://social.msdn.microsoft.com/Profile/en-US/?user=Saroj%20JhaException encountered using SPSecurity.RunWithElevatedPrivileges<p align=left><font face=Arial size=2></font> </p> <p>Thanks. Cris</p> <p align=left>It Worked Well.</p> <p align=left> I too was struck with this problem for long.</p> <p align=left>reg,</p> <p align=left>saroj</p> <p align=left> </p>Wed, 28 Nov 2007 14:27:43 Z2007-11-28T14:27:43Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#418e42fe-081a-4ea0-a179-b3f81bcc05edhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#418e42fe-081a-4ea0-a179-b3f81bcc05edVeda Prakash Girmajihttp://social.msdn.microsoft.com/Profile/en-US/?user=Veda%20Prakash%20GirmajiException encountered using SPSecurity.RunWithElevatedPrivileges<p align=left><font face=Arial size=2></font> </p> <p>Thanks eddy.</p> <p align=left> </p> <p align=left>Above solution worked out very well</p>Wed, 30 Jan 2008 05:29:57 Z2008-01-30T05:29:57Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#d0123cfb-56bd-49e9-9824-1d716d401936http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#d0123cfb-56bd-49e9-9824-1d716d401936Gavin Barronhttp://social.msdn.microsoft.com/Profile/en-US/?user=Gavin%20BarronException encountered using SPSecurity.RunWithElevatedPrivileges<span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">I k</span>now how you can work arround &quot;Microsoft.SharePoint.SPException: The security validation for this page is invalid.&quot; <img alt=Smile src="http://forums.microsoft.com/MSDN/emoticons/emotion-1.gif"><br><br>Just disable form digest validation for a little bit <img height=19 alt=Wink src="http://forums.microsoft.com/MSDN/emoticons/emotion-5.gif" width=19><br><br> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <div style="margin-left:40px"><span id="_ctl0_MainContent_PostFlatView"><span> <p>SPWebApplication webApp = currentWeb.Site.WebApplication;</p></span></span></div> <div style="margin-left:40px"><span id="_ctl0_MainContent_PostFlatView"><span> <p>bool formDigestEnabledStatus = webApp.FormDigestSettings.Enabled;</p></span></span></div> <div style="margin-left:40px"><span id="_ctl0_MainContent_PostFlatView"><span></span></span><span id="_ctl0_MainContent_PostFlatView"><span> <p>webApp.FormDigestSettings.Enabled = false;</p></span></span></div> <p><span id="_ctl0_MainContent_PostFlatView"><span> <p style="margin-left:120px">//Do stuff to <span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">BreakRoleInheritance here</span></span> </p></span></span> <p></p> <p style="margin-left:40px">webApp.FormDigestSettings.Enabled = formDigestEnabledStatus;</p></div></div></span> <div style="text-align:left"><span id="_ctl0_MainContent_PostFlatView"><span></span></span><span id="_ctl0_MainContent_PostFlatView"><span></span></span><br><span id="_ctl0_MainContent_PostFlatView"><span> <p> </p></span></span></div><span id="_ctl0_MainContent_PostFlatView"><span></span></span><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">Although given the suggestions that others are making that my uncover other issues that their posts hopefully resolve<br></span></span>Wed, 30 Jan 2008 07:05:08 Z2008-03-01T22:06:20Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#e57df5ae-1143-48ae-9931-789cc6fca963http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/63106621-cbde-44d9-b3cf-07bddc0856f1#e57df5ae-1143-48ae-9931-789cc6fca963David Lozzihttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20LozziException encountered using SPSecurity.RunWithElevatedPrivileges Awesome, thank you.<hr size="1" align="left" width="25%">David Lozzi<br> <a href="http://www.delphi-ts.com">Delphi Technology Solutions</a><br> <a href="http://www.lozzi.net">Blog</a>Fri, 31 Oct 2008 19:36:54 Z2008-10-31T19:36:54Z