Permission levels in SharePoint are very important thing. They define actions which specified users can perform on sites, lists etc. But, in vast amount of applications, there is a way to bypass permission checking and become a “god” who can do almost everything. I’m talking about SPSecurity.RunWithElevatedPrivileges method. This method lets’ You get a reference to the SPSite object and with it You can access any of the site collection elements with administrator rights.

The usage is very simple:

SPSecurity.CodeToRunElevated elevatedMethod = new SPSecurity.CodeToRunElevated(ParameterlessMethod);
SPSecurity.RunWithElevatedPrivileges(elevatedMethod);

To make it more elegant and ommit creating a new method definition we can use an anonymous method:

SPSecurity.RunWithElevatedPrivileges(delegate() { //Code to run elevated });

This way You can for example get a reference to the list, which You normally don’t have access to and change its’ contents:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using(SPSite site = new SPSite(SPContext.Current.Site.ID))
{
SPList list = site.RootWeb.Lists["myList"];
list.Title = "myListNewTitle";
list.Update();
}
});

IMPORTANT!
Remember not to use SPContext.Current.Site when acquiring the reference to the site object in using clause:

using(SPSite site = SPContext.Current.Site)

The code above will dispose the site object after leaving the using clause and delete the whole site colletion as well.