Elevated Privileges in SharePoint 2007

This is a quick note on incorporating ElevatedPrivileges in custom webparts, event handlers, etc.

Inside the CreateChildControls() method, register the Button Click method reference or any other event.


Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
AddHandler Button1.Click, AddressOf Button1_Click
 End Sub

Inside the Button Event, raise the ElevatedPrivileges in the following way.


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
SPSecurity.RunWithElevatedPrivileges(AddressOf MethodWithElevatedPermission)
 End Sub

Private Sub MethodWithElevatedPermission()
' Add desired business logic here
 End Sub

I have not had the chance to test this on SharePoint 2010 custom application, however I am pursuaded to think it will work for SharePoint 2010 as well providing SharePoint 2010 embraces SharePoint 2007 Object Model.

For C# reference:
http://msdn.microsoft.com/en-us/library/bb466220(v=office.12).aspx