Automating deployment of reusable workflows in Nintex Workflow 2010

One of the options for creating workflows in Nintex Workflow 2010 (NW2010 for short) is to associate workflows  with Content Types.This category of workflows is known as Reusable workflows.

As stated in Nintex Workflow 2010 User Manual

A resusable workflow template allows the workflow to be used on a content type, list or library through the
default SharePoint Workflow settings option.
Reusable workflow templates can be created for use within a single site or an entire site collection

In our case we are only interested in reusable workflows for content types.

Let’s disscuss the scenario when we need to automate the deployment of reusable workflow. It may take place for example, when there are several environments. The approach discussed here allows to deploy workflows during feature activation.

NW2010 API contains publishing infrastucture (Nintex.Workflow.Publishing namespace) that allow to programatically publish workflow templates.
Class Publish contains  method

that allows to associate workflow using ContentTypeId parameter.

The method below demonstrate how to deploy reusable workflow file by passing it file name, contenttype name and workflow name(NWFMappingEntry parameter)  using Nintex API PublishAWorkflow method.

Project that demonstrates how to deploy Nintex reusable workflows may be found here

Advertisements

10 thoughts on “Automating deployment of reusable workflows in Nintex Workflow 2010

  1. Hi,
    I’ve been looking at your code and have downloaded the sample application, many thanks – however the call to Nintex’s PublishAWorkflow() method is throwing an exception “Object reference not set”. I am using the trial version of Nintex at present (v2.3.3.0) – could you tell me what the Nintex version is you have installed where this code is working?

    Thanks again

    • First of all, you have to modify NWFContent\NWMappings.xml file, element BindingName should correspond to existing content type name.
      Second, i also recommend you to create workflow in Nintex Workflow(empty workflow would be enough) and export it to file (save it in folder NWFContent). Then update in NWFContent\NWMappings.xml element WorkflowFileName with your file name.
      Try to deploy.

      P.S. I’ve tested with NW2010 v2.3.3.0 (with language pack installed).

      • Hi again vgrem, thanks for the swift reply..

        I’ve modified your project and created a site collection reusable nwf that just logs “hello world” to history. I’ve associated it with Document and exported then modified NWFMapping.xml but.. I’m still getting the object reference not set exception when PublishAWorkflow() is called.

        I’ve retried web application activation in Central Admin and I’ve got every Nintex feature enabled in the target site collection but the same thing happens. I’ve stepped through the code and everything seems ok, the correct objects are created etc…

        Comparing your nwf xml file to mine, I notice yours has a element with a value of 2111 whereas mine is 2330 and mine has an extra element – so perhaps this might be something to do with my problem?

        Thanks again

      • Hi,
        this time i’ve created workflow template(nwf) in NW2010 2.3.3.0 exactly the same way as you described (see updated sample application) but still can’t reproduce the issue with NullRerenceException when PublishAWorkflow is called.
        Please, make sure that in your case PublishAWorkflow is not called, for example from console app, because in that case SPContext.Current is not initilized.
        If it not helps, we need to take a look at ULS & stack trace.

      • Hi Vgrem, you are right – I originally was hoping to deploy using powershell but the exception is also thrown for me when Visual Studio auto-activates the feature: I’m getting the error when I hit F5 as SPContext.Current is also null. If I activate the feature using the UI, I dont see an exception thrown and the workflow is correctly published- so there is something in the PublishAWorkflow() method that relies on SPContext.Current, (and also something strange occuring in my dev environment!); many thanks.

  2. Hi Vgrem, Thanks for this post.
    I have tried to add the solution with PowerShell and to deploy it through the Central Administration, but I get the following error : ERROR: System.ArgumentNullException: Value cannot be null. Parameter name: type at System.Activator.CreateInstance(Type type, Boolean nonPublic).
    Does this come from one of the parameters ? Maybe “(ImportContext)null” ?
    Thanks in advance for your help.

  3. I know this is an old post but thought I would post up the solution for what I think is the error that everyone is having.

    The NWMapping.xml file should be filled in as

    Name of the workflow
    NWFFiles/workflowFiles.nwf
    Content Type Name

    Now in my instance I made a site reusable workflow to work on all items so Binding Name needs to be null

    This will lead to an exception in the class NWFAdapter, specifically the method PublishReusableWorkflow. This is because it always expects a Content Type to resolve.

    I changed the code to the following.

    public void PublishReusableWorkflow(NWFMappingEntry mapping)
    {
    SPContentType ct = web.ContentTypes[mapping.BindingName];
    SPContentTypeId ctId;

    if(string.IsNullOrEmpty(mapping.BindingName))
    {
    ctId = SPContentTypeId.Empty;
    }else
    {
    ct = web.ContentTypes[mapping.BindingName];
    if(ct == null)
    {
    throw new Exception(“Content Type not found”);
    }
    ctId = ct.Id;
    }

    string workflowName = mapping.WorkflowName;
    string pathToNWF = Path.Combine(properties.Definition.RootDirectory, mapping.WorkflowFileName);
    byte[] workflowData = File.ReadAllBytes(pathToNWF);
    string workflowFile = Utility.ConvertByteArrayToString(workflowData);

    while ((int)workflowFile[0] != (int)char.ConvertFromUtf32(60)[0])
    workflowFile = workflowFile.Remove(0, 1);

    ExportedWorkflowWithListMetdata workflowWithListMetdata = ExportedWorkflowWithListMetdata.Deserialize(workflowFile);
    string xmlMessage = workflowWithListMetdata.ExportedWorkflowSeralized;
    SPListCollection lists = web.Lists;
    Dictionary dictionary = new Dictionary(lists.Count);
    foreach (SPList spList in lists)
    {
    if (!dictionary.ContainsKey(spList.Title.ToUpper()))
    dictionary.Add(spList.Title.ToUpper(), spList.ID);
    }
    foreach (var listReference in workflowWithListMetdata.ListReferences)
    {
    string key = listReference.ListName.ToUpper();
    if (dictionary.ContainsKey(key) && !dictionary.ContainsValue(listReference.ListId))
    xmlMessage = xmlMessage.Replace(Utility.FormatGuid(listReference.ListId), Utility.FormatGuid(dictionary[key]));
    }
    var exportedWorkflow = WorkflowPart.Deserialize(xmlMessage);
    foreach (var config in exportedWorkflow.Configurations.ActionConfigs)
    WorkflowRenderer.ProcessActionConfig(config);

    Guid listId = Guid.Empty;
    bool validateWorkflow = true;
    Publish publish = new Publish(web);

    publish.PublishAWorkflow(workflowName, exportedWorkflow.Configurations, listId, web, (ImportContext)null, validateWorkflow, ctId, string.Empty);
    }

    Sorry about the formatting.

  4. Hi,
    This works fine if set in a feature receiver and activated via the interface.
    But when activating the feature via powershell, it seems PublishAWorkflow is throwing a NullReferenceException. This is because Nintex is using an SPContext.Current somewhere internally, which, in powershell is obviously null.

    The solution would be to create a dummy SPContext, but this only yields in a “Updates dissallowd on GET requests” error, although all SPWeb objects have AllowUnsafeUpdates = true.

    Is there a solution or a workaround to this ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s