Catching Access Denied Exceptions

When attempting to debug a recent solution that was generating “Access Denied” errors for our users, one of our developers discovered some nuances to trapping these exceptions. The following are his notes on the issue.

Access Denied exceptions are not caught by the Try Catch method unless you set up security to properly handle it.  If you just wrap a normal try catch around code and get an Access Denied exception, it will blow past the try catch and throw the exception anyway.

I had a situation where I needed to check for the existence of a document in a library within another users MySite.  All I needed to do was to read the library and display a link to the first file found in that library in a custom web part.  However, if the library did not exist, an Access denied error was thrown to the user.  I wanted to catch this error and simply place a file not found message in the web part where I would normally display the link to the file.  Of course, the try catch would not capture this error without a little help.

The solution is to set CatchAccessDeniedException = false before you execute the read.

The steps are:

  1. Save the current status of CatchAccessDeniedException to a temporary variable 
  2. Set the CatchAccessDeniedException = false
  3. Execute code to access library
  4. In the finally clause, set CatchAccessDeniedException back to the value of the temporary variable

See Code below:

MSDN – SPSecurity.CatchAccessDeniedException

List Throttling – Filter Not Working?

September 19, 2011 Leave a comment

“This view cannot be displayed because it exceeds the list view threshold filter”

Though it is somewhat documented in this blog post, I wanted to reiterate and clarify the interaction of list throttling and list views. In cases where you have a large list (> 5000 items), the first indexed column specified in the filter is used to return the subset of items used to create the view. If the first condition in your filter doesn’t return less than 5000 items, throttling kicks in. See the link above for a more detailed explanation.

Content Type Oddness – Switcharoo

I ran across an interesting “feature” today in SharePoint 2010. I’ve spent the best part of 2 days trying to figure out why some new content types I’ve created don’t behave as expected. When I created a document based on these content types and saved them, SharePoint kept reverting them back to the “Document” content type.

As I eventually discovered, it was related to how I was supplying the Document Template. I was giving a URL  inside of SharePoint to the document I wanted to use as a template for the content type. Apparently it was reverting my newly minted documents to the content type of the template being used. If I upload my template from disk… voilà, problem solved.

User Profile Sync – Initiating in Powershell

As you might have already discovered, some changes in user profiles require a full synchronization to populate across sites and to and external synchronization connections (should you be exporting). In my case, user profile photos were not being exported to the AD thumbnailPhoto attribute unless I did a full sync. So I needed a way to automate a scheduled full synchronization. Enter PowerShell. Though I couldn’t find this documented anywhere, I did a little digging and found a method that seems to do the trick. Below are the two lines of code that kick of a synchronization. Subsitute the guid in the script below with the guid for your upa.

$upa = Get-SPServiceApplication 92ff4bfa-1ad0-43e2-ba29-683657aee811
$upa.StartImport($true)

Save the script… create a scheduled task. YMMV.

Disclaimer: A full profile sync can be resource intensive. The size of your directory can cause a full sync to take a good bit of time. Schedule wisely.

Categories: Uncategorized

Spell Checking – Workaround

I’ve got a response from Microsoft on the spell checking issue and it includes a workaround. I’m posting it here in the hope that others with this issue might benefit.

ISSUE

=======================================

While editing a custom page and using the spell checking functionality, the spell checker detects errors on the people picker control added to the custom page.

OBJECTIVE

=======================================

Resolve the above issue

WORK DONE SO FAR

=======================================

Scoping calls and issue verification

Research functionality and frequency of issue

Issue reproduced on Microsoft

Source code investigation

Workaround testing

ROOT CAUSE OF THE ISSUE

=======================================

During the page editing process, SharePoint looks for text that are spell checking candidates. The text is included unless the control is explicitly marked for exclusion by a non-public attribute or by having its class set to ‘ms-spellcheck-false’.

The people picker control fails both test and its text, in xml format, is then included in the spelling chunks.

WORKAROUND

=======================================

We’ll add a small custom code to override the above behavior and exclude the control from the spell checking.

Please follow these steps to implement it:

Launch SharePoint Designer and from the File Menu, choose Sites.

Open your site and from the site objects menu on the left, select Page Layouts.

From the list of page layouts on the right panel, right click on the page with the issue (the one containing the people picker control) and select “Edit File in Advanced Mode”

Note: if the page is not checked-out, you’ll get a prompt for it. Click ‘Yes” for it.

For the next steps, refer to the following example, as it looks like on my test page:

On the HTML that defines the control, add an ID property if one doesn’t already exists. You’ll need this ID for the next step.

As an example, here’s how my test page is:

.

.

.

<table>

   <tr>

      <td>

         Contact: <SharePointWebControls:UserField FieldName=”PublishingContact” runat=”server” ID=”PeoplePicker1″ />

      </td>

   </tr>

</table>

.

.

.

Before the last statement on the page( the </asp:Content> tag), add the following script:

       <script type=”text/javascript”>

       _spBodyOnLoadFunctionNames.push(“OverrideSpelling”);

       function OverrideSpelling(){ 

              var $fld_in = document.getElementsByTagName(‘input’); 

              var $fld_tx = document.getElementsByTagName(‘textarea’); 

              for( $i = 0; $i < $fld_in.length; $i ++ ) {

                     var $inputName = $fld_in[$i].getAttribute(‘name’);   

                     if($inputName.indexOf(‘PeoplePicker1′) != -1) {

                           $fld_in[$i].className = ‘ms-spellcheck-false’;   

                     }

              }

              for( $i = 0; $i < $fld_tx.length; $i ++ ) {

                     var $inputName = $fld_tx[$i].getAttribute(‘name’);   

                     if($inputName.indexOf(‘PeoplePicker1′) != -1) {

                           $fld_tx[$i].setAttribute(“excludeFromSpellCheck”,”true”);   

                     }

              }

       }     

       </script>

Save the page and try it out now.

The page should exclude the people picker control from the spelling now.

Spell Checking the HTML

It appears we have found a bug in SharePoint regarding use of the PeopleEditor control. When that control is placed on a page, any spell checking is done to the HTML as well and thus reports an erroneous number of spelling mistakes. Microsoft has been made of the issue but no official schedule for a fix has been established.

Categories: Bugs Tags: , , ,
Follow

Get every new post delivered to your Inbox.