In a Webpart a user开发者_开发百科 without any privileges needs to read and update a Sharepoint list.
Elevating privileges works ok for reading the list, but when I try to update the same list, throws a Exception. How is it possible to update a list with elevated privileges?
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite oSite = SPControl.GetContextSite(HttpContext.Current);
SPWeb oWeb = oSite.OpenWeb();
oWeb.AllowUnsafeUpdates = true;
SPListItemCollection listItems = oWeb.Lists["nameList"].Items;
SPListItem item = listItems.Add();
...
item.Update(); // Throws Exception
});
elevated privilages is used to add edit and delete all operations so thier must be some other problem kindly provide the exception details
The problem was the creation of the SPSite.
The correct code:
SPSite oSite = new SPSite(SPContext.Current.Site.ID);
SPWeb oWeb = oSite.OpenWeb(SPContext.Current.Web.ID);
listItems = oWeb.Lists["nameList"];
精彩评论