Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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;
}
share|improve this answer
    
worked. thanks. –  Ole Albers May 25 '12 at 16:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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