In a sharepoint project I save data to a list.
When the Administrator sets the database to ReadOnly, I want to capture the Exception that occurs when trying to write to that list and prevent the system from writing into the list for an hour. This is the code:
public bool ReadOnlyDb
{
get
{
HttpSessionState session = HttpContext.Current.Session;
if (session != null)
{
return session["READONLY"] != null && ((DateTime) session["READONLY"]) > DateTime.Now.AddHours(-1);
} else
{
return false;
}
}
}
if (!ReadOnlyDb)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID))
{
try
{
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList logList = web.Lists["Log"];
SPListItem addableItem = logList.AddItem();
addableItem["Title"] = message;
addableItem.Update();
web.AllowUnsafeUpdates = false;
web.Dispose();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Log to List DISABLED für Session. (READONLY?)");
System.Diagnostics.Debug.WriteLine(ex);
HttpSessionState session = HttpContext.Current.Session;
if (session != null)
{
session["READONLY"] = DateTime.Now;
}
}
}
});
}
}
The code works as expected. The exception is thrown in read-only mode and the session-variable "READONLY" is set.
But when the Exception is thrown (as expected) I get a sharepoint "access denied" - message ("sign in as a different user?"). When I reload that page, the session variable is set correctly, so the last line of my method is run.
Also there is no exception thrown in visual studio. Where am I missing something?