Researching Site Template Structure with SPD workflows

Introduction

Usually, SharePoint development process looks as follows. You create a new site which is based on one of the standard SharePoint templates (Team Site, Blog, Project Site, etc.) after that, you work hard and deeply customize it (add new lists, views, workflows, customize UI) and finally you will get a complete business solution.

But there is another question: “How to deliver your solution to a client?”. One of the answers to this question is to use site templates. You can save the customized site to WSP package and deploy the package to the customer’s environment.

The issue with deployment of template with custom workflow actions

Looks very interesting and useful, but in SharePoint’s world, the Devil is in the detail. For example, if you’re using custom workflow actions in your workflows you will get an error during deployment process:

This not informative message means that the SharePoint tried to publish your workflow, but it can’t find custom workflow action. I saw this issue for several of our clients and after research, we have found the reason. It turned up that during deployment process SharePoint doesn’t trigger feature event receivers. Event receiver doesn’t deploy workflow actions and SharePoint throws an error during publishing process (because it can’t find workflow actions used in the workflow).

As result of our research, we have found two workarounds how to solve the issue and deploy site template with workflows including custom actions.

The first way: You need to save workflows as templates. Then remove them from the site (you are always able to restore them from saved templates). Deactivate a feature which contains custom actions (In my case it is Workflow Actions Pack) and save the site as a template. Thus, you have the site template and the templates of workflows as separate WSP packages. To deploy such site you need to create the new site using your template (without workflows). Then activate feature with your custom actions. Then deploy site workflows templates which you saved earlier.

The second way: You can save the site as a template and manually edit WSP package. You need to move workflows to another feature that will be activated manually, also you need to remove workflow actions from modules (because they are deployed via separate WSP package). This is the preferred way, but it requires strong developers skills. I will describe it below. And together we will try to automate the solution.

Typical structure of a WSP package

Before can start playing with the inner content of WSP package we should understand its structure.
As you maybe already know any WSP package is renamed CAB archive. If we open it we should see something like this:

The main file “manifest.xml” contains the list of features, each feature is located in a separate folder. You may notice that SharePoint divided features by executed functions (CustomActions, ListInstanes, PropertyBags, etc..)
The feature definition files are “Feature.xml” and “Elements.xml”. Additionally, a feature may deploy other files, all deployable files are described in the feature definition file.

Typical feature definition looks like:

As you can see it is pretty simple. The Feature has a Title, ID, Description, link to manifest and a list of included files. If you want to know a little bit more you can read MSDN documentation.

Additionally, I want to highlight ONet.xml file, it contains the list of features that will be activated during deployment process. Apart from features in the package it contains references to dependent features from other packages. An example from this file is shown below:

The Solution

Now when we studied the structure of typical WSP package we can move forward. I want to write a simple console utility which will repair the WSP package and it will move all workflows to separate feature.
To start we will divide all work into separate stages:

  • Create new directory for workflows feature
  • Create Feature.xml and Elements.xml definitions for the workflow feature
  • Browse all other features and remove custom workflow actions (because the workflow actions are deployed via separate WSP package, if we don’t do it in SharePoint Designer we will see duplicated workflow actions)
  • Walk through all other features and move workflows into our new workflow feature.
  • Repackage WSP package.

To ensure that the solution will work we can walk through these steps manually, but the next step we will automate this process and in next part of the article I will show you the main functions of the console program that you can use.

I do not want to clutter the article with a lot of code and I will just show examples of the main functions.
To add a new feature we will create Feature.xml and Elements.xml in a separate folder inside WSP package.

Firstly we need to create new workflows feature, to do this I used the following code:

Then we need to process the manifest file and iterate through the all features:

For each feature, I call a list of working methods that will process a feature individually

The main thing here in the methods RemoveCustomActions, RemoveModules, RemoveDependentFeatures and MoveWorkflowModules. They work with input XElements and also move files physically. Then we just save the Feature and Elements files.

As result of our work the structure of WSP package will look as follows:

Conclusion

As aconclusion of this small research, we’ve got a small console utility that will help you to prepare WSP package to deploy to another SharePoint environment.

All source code is available on GitHub and you can research it deeper or write something useful for your case.

Feel free to comment the article. I will be glad to answer interesting questions.

P.S. If you’re not a developer and you want to just solve the issue, you can read the following article where is shown how you can use this small utility.