Thursday, August 2, 2012

Lessons Learned Using the SharePoint Service Locator Pattern

Having wrestled with maintaining separate code bases for both SharePoint 2007 and 2010 I finally got to the point where I had some time to rethink strategy and that's when I came across Sohel's blog which explained a solution using the SharePoint Service Locator Pattern. Having just finished implementing this pattern I now can see some of the *huge* benefits, this approach provides. Here are a few:

  • Creating loose decoupling of code that is modular which enables a developer to make changes to dependencies without needing to recompile the existing code base. 
  • Easy write mocks and stubs for unit tests due to the dependency injection provided by the Service Locator 
  • Consolidation of repetitive code into a single code base 

After implementing this solution I wanted to list out some issues (work in progress) that I came across and how I fixed them:

Issue #1
SharePointServiceLocator.GetCurrent() threw a null reference exception

Solution to Issue #1
I was making calls to SharePointServiceLocator.GetCurrent() within the constructor of my application pages in SharePoint. For example, if I have a class named: MyClass, the constructor would look like:

public MyClass()

Moving the call, SharePointServiceLocator.GetCurrent(), to another location outside the constructor fixed the problem. Apparently the ServiceLocator instance is not instantiated until later in the page load cycle.

Issue #2
"Access Denied" error when registering mappings

Solution to Issue #2
This can happen when adding mappings at the Farm level (default setting for SP 2010 and the only option for SP 2007) in the SPWeb context. The solution is to create a Farm scoped feature and add register the mappings within the 'FeatureInstalled" event handler.

Issue #3
Type mappings are cleared from cache at the site collection level but not at the Farm level

Solution to Issue #3
Chris Keyser discussed this issue and a work around here:

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.

 catch(Exception ex)  
  //Handle Exception  

 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")) {

      // delete Web Part template files
      foreach (SPFile file in FilesToDelete) {

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

Friday, April 20, 2012

Expanding VMWare Hard Disk with VMWare Player 4.0 Installed

After I finished setting up my VMWare hard disk using VMWare Player 4.0 I was down to less than 1 GB of hard disk space left on the player. The instructions to expand the disk only went up to version 3.x and I was unable to locate vmware-vdiskmanager to perform the operation. Here are the steps you need to take to expand your hard disk with VMWare Player 4.0:

1) Download and install VMware Virtual Disk Development Kit

2) Browse to the VVDDK bin directory and run: vmware-vdiskmanager -x 60Gb vm.vmdk
   - Adjust the size of the disk from 60 Gb to whatever suits you
   - Be sure to update the path of your VMDK file

3) Go here to update the guest operating system so it knows the new disk size if it is partitioned