Pages

Showing posts with label Best Practice Tip. Show all posts
Showing posts with label Best Practice Tip. Show all posts

Tuesday, July 17, 2012

Managing Exceptions Using Delegates

Now that you have perfect code that follows Microsoft's Exception Management Practices for SharePoint ;) you may be wondering..."Is there a way I can get rid of all the *extra* try / catch / finally blocks?" Well this post is to help provide an option that I recently implemented (as a side note if you have other options please feel free to comment below).

Don't let this example steer you toward catching general exceptions as the norm. This example is mainly to help provide some guidance on abstracting exception handling but not at the risk of generalizing exception management.

-The first step is to become familiar with Delegates in C# (if you are not already familiar). 

-Next you will need to create a class with at least two methods that will serve as the Exception Manager. Each method will receive a Delegate, as a parameter, that points to the Routine that is being wrapped. The first method will only handle exceptions for Actions (nothing is returned by the Routine). The second method will handle exceptions for Functions (something is returned by the Routine)

-Finally you will need to wrap a Routine call using a Delegate pointer to that routine and pass it to either method in the Exception Manager class.

You can see the result in the following example. You can also download a code sample from my SkyDrive.

Before:
 try  
 {  
  DoSomething();  
 }  
 catch(Exception ex)  
 {  
  //Handle Exception  
 }  
 finally  
 {  
 //Continue...  
 }  

After:
 ExceptionManager.ProcessFunc(() => DoSomething());  

Monday, May 21, 2012

SharePoint 2010 Web Part Feature Clean Up

Reading through "Inside Microsoft SharePoint 2010" I came across a section on page 192 titled, "Deleting Web Part Template Files at Feature Deactivation". I thought the suggestion to remove the .webpart template file from the Web Part gallery was worth writing about. In case you never tried this use case, attaching a SharePoint Web Part to a feature in SharePoint only supports adding a .webpart template file to the Web Part gallery upon feature activation. Deactivating the feature doesn't remove the .webpart template file which is why I recommend adding a feature event handler that removes this file from the gallery when deactivating your Web Part feature.  Here is the sample code segment taken from the book referenced above to remove the template file:


public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {

      SPSite siteCollection = (SPSite)properties.Feature.Parent;
      SPWeb site = siteCollection.RootWeb;

      List<SPFile> FilesToDelete = new List<SPFile>();
      // figure out which Web Part template files need to be deleted
      SPList WebPartGallery = site.Lists["Web Part Gallery"];
      foreach (SPListItem WebPartTemplateFile in WebPartGallery.Items) {
        if (WebPartTemplateFile.File.Name.Contains("put your .webpart file title here")) {
          FilesToDelete.Add(WebPartTemplateFile.File);
        }
      }

      // delete Web Part template files
      foreach (SPFile file in FilesToDelete) {
        file.Delete();
      }

    }
As a developer in training I welcome any constructive feedback. Please feel free to comment below. Thanks.