Just to clarifiy, runwithelevatedprivlages is not impersonation:
a good example is an anonymous user who you want to edit (add to) a sharepoint list that they dont have access to programmaticaly.
runwithelevatedprivlages will not work as anonymous users wouldnt have anything to elevate, this is where you need to impersonate an account that does have access. runwithelevatedprivlages works well with logged in users who dont have the right permissions.
as an example I will show you impersonation first:
to impersonate you need to get the system token first and make sharepoint think that your that person, anything within the spsite that your going to impersonate will be under that user and not the current user your using, the method below is getting the system account token with elevated privlages (for anonymous users):
/// <summary>
/// get the system token from the list of users to elevate spsite
/// </summary>
/// <param name="spSite"></param>
/// <returns></returns>
protected static SPUserToken GetSystemToken(SPSite spSite)
{
SPUserToken res = null;
bool oldCatchAccessDeniedException = spSite.CatchAccessDeniedException;
try
{
spSite.CatchAccessDeniedException = false;
res = spSite.SystemAccount.UserToken;
}
catch (UnauthorizedAccessException)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSPSite = new SPSite(spSite.ID))
res = elevatedSPSite.SystemAccount.UserToken;
// (***)
});
}
finally
{
spSite.CatchAccessDeniedException = oldCatchAccessDeniedException;
}
return res;
}
to use this method you would need to do the following:
SPUserToken sysToken = null;
using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
{
//get the system token from the method GetSystemToken and passing the spSite that you want to run under
sysToken = GetSystemToken(spSite);
using (SPSite impersonatedSite = new SPSite(siteUrl, sysToken))
{
//we are impersonating the [@"SHAREPOINT\SYSTEM"] account
using (SPWeb web = impersonatedSite.OpenWeb())
{
//do your code here as the impersonated account like updating a list
}
}
}
this will work where RunWithElevatedPrivileges does not work :) as you can see there is suttle differences between the two, impersonation is not using the application pool account but rather any account that you want to impersonate. RunWithElevatedPrivileges only uses the application pool account and has some limitation to what access it has (your only elevating the current account!! not impersonating the app pool account)
msdn RunWithElevatedPrivileges explained
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
msdn Impersonation explained
http://msdn.microsoft.com/en-us/library/aa543158(v=office.14).aspx
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms691341(v=vs.85).aspx
for the op, I would suggest as others and use RunWithElevatedPrivileges as it should be more than enough :)
hope this helps :)