mtelligent

View Original

Overcoming the limitations of the SharePoint Solution Generator

When you install the Visual Studio 2005 extensions for Windows SharePoint Services 3.0 (VSeWSS), you get a neat little tool called the SharePoint solution generator. You can point this tool at any existing sharepoint site and it will produce a Visual Studio Solution that includes the major components of the site. When you compile the solution you get a SharePoint Solution file (wsp file) that can then be used to deploy the solution as a Site Template on any SharePoint server using the STSADM tool. Once installed, you just have to provision a new site using the template.

The tool itself does save a lot of work. It just doesn't do everythng that you might expect. For a recent project, I configured a custom site, got it looking and working exactly how I wanted it and then pointed the tool at the site I created. I then compiled the solution and deployed it to a test server. I found that a lot of my customizations were gone. I'll admit that I thought I might have to write custom code for some of this stuff, but others I was a bit surprised that it wasn't able to handle. The list of issues for my particular custom site included:

  • Lookup Columns disappeared from any list that had them.
  • Customizations to the Quick Launch navigation for the site were lost
  • WebParts that I configured on pages were gone.
  • Custom ListItem Event Receivers were no longer attached to the lists

Besides this, I also needed to create a couple of custom groups, populate some custom data into some of the lists and install a timer job as part of the installation.

Although none of these things are handled automatically, the solution file does give you a  hook to perform custom actions while the site is being provisioned. In the generated solution file is a folder called "Site Provisioning Handler" which contains a class called "SiteProvisioning.cs." In this class is one method "OnActivated" which allows you to add custom code. This method is called when a new site is being provisioned using the Site Definition. It accepts one parameter of type SPFeatureReceiverProperties named properties. Using this parameter you can find get the SPSite and SPWeb objects using the following code:

SPWeb web;
SPSite site;

if (properties.Feature.Parent is SPWeb)
{
    web = properties.Feature.Parent as SPWeb;
    site = web.Site;
}
else
{
    site = properties.Feature.Parent as SPSite;
    web = site.RootWeb;
}

Once you have the site and web, you can start working on writing code to put back the customizations that the Solution Generator wasn't able to automate. I already posted about how to fix the Quick Launch navigation in this post. Over my next few posts I will detail just how I was able to put back the rest.

Stay tuned...