I get an error trying to use SPSecurity.RunWithElevatedPrivileges in a webpart.
The webpart iterates through all sites, and populates an SPGridView with the result. This works fine using the System account, but when I login using Forms Authentication I get the Access Denied page, and an UnauthorizedAccessException.
I tried to use the SPSecurity.RunWithElevatedPrivileges, but I get this error:cannot convert from 'anonymous method' to 'Microsoft.SharePoint.SPSecurity.CodeToRunElevated'
The method:
(SiteHyperLink is just a small class with two properties Title and Url, and ConvertURLsToHyperLinks is a Regex.Replace method for the urls)
private ArrayList ProcessHyperLinks()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite mySite = SPContext.Current.Site)
{
SPWebCollection allWebs = mySite.AllWebs;
ArrayList returnArrayList = new ArrayList();
foreach (SPWeb subWeb in allWebs)
{
SiteHyperLink sh = new SiteHyperLink(
subWeb.Title, ConvertURLsToHyperLinks(subWeb.Url));
returnArrayList.Add(sh);
}
return returnArrayList;
}
});
}
Seems I can't do it like this. Any ideas or solutions would be appreciated very much.