A Webpart needs to access a Sharepoint List (read mode). If the user is admin, there isn't problem (works as espected), but if the user hasn't permissions to access, I must use "RunWithElevatedPrivileges" method.
The problem is that seems that the query don't return the correct results. What I'm missing?
SPList demoList = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite oSite = SPControl.GetContextSite(HttpContext.Current); // ADDED
SPWeb oWeb = oSite.OpenWeb(); // ADDED
demoList = oWeb.Lists["nameList"];
});
// demoList has 3 Elements (admin and no admin user) OK
SPListItemCollection collListItems = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPQuery oQuery = new SPQuery() { Query = "<OrderBy><FieldRef Name='Date' Ascending='False' /></OrderBy>" };
collListItems = demoList.GetItems(oQuery);
});
//
//IF ADMIN
//collListItems.Count ==>3
//IF NO ADMIN
//collListItems.Count ==>0