1

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?

1

When you're trying to catch AccessDenied to handle it yourselves you should set SPSecurity.CatchAccessDeniedException to false:

bool originalCatchValue = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
try
{
   // details omitted
}
finally
{
   SPSecurity.CatchAccessDeniedException = originalCatchValue;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.