SharePoint.NintexDeployment.Infrastructure.NWFAdapter.PublishReusableWorkflow C# (CSharp) Method

PublishReusableWorkflow() public method

Publish Reusable Workflow
public PublishReusableWorkflow ( NWFMappingEntry mapping ) : void
mapping NWFMappingEntry
return void
        public void PublishReusableWorkflow(NWFMappingEntry mapping)
        {
            SPContentType ct = web.ContentTypes[mapping.BindingName];
            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<string, Guid> dictionary = new Dictionary<string, Guid>(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<ExportedWorkflow>(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, ct.Id, string.Empty);
        }

Usage Example

 private void DeployWorkflows(SPFeatureReceiverProperties properties)
 {
     var workflowAdapter = new NWFAdapter(properties);
     //Workflows
     var workflowMappings = workflowAdapter.RetrieveWorkflowMappings("NWMappings.xml");
     foreach (var nwMappingEntry in workflowMappings)
     {
         try
         {
             workflowAdapter.PublishReusableWorkflow(nwMappingEntry);
             WorkflowUtilities.BindWorkflow((SPWeb)properties.Feature.Parent, nwMappingEntry.BindingName, nwMappingEntry.WorkflowName);
         }
         catch (Exception ex)
         {
             NWLoggingService.WriteError(ex);
         }
     }
 }