Archive for January, 2012

SharePoint: Associate Workflow with a list programmatically

Scenario:

We need to associate a workflow with a list or create an instance of workflow for a list using SharePoint Object Model (OM)

Resolution:

In order to associate workflow with a list, we will be using CreateListAssociation method of SPWorkflowAssociaition class. We need following four values:

  • baseTemplate – The workflow template on which to base this workflow association.
  • Name – The name to give this workflow association.
  • taskList – The task list on which to create workflow tasks for this workflow association.
  • historyList – The list to which to log workflow history events

Code is as under:

using(SPSite spSite = new SPSite(“SiteURL”))

{

SPWeb spWeb = spSite.OpenWeb();

SPList assocList = spWeb.Lists[“NameOfTheList”];

SPWorkflowTemplate wflTemplate = spWeb.WorkflowTemplates.GetTemplateByName(“NameOfWorkflowTemplate”, System.Globalization.CultureInfo.CurrentCulture);

SPList wflTaskList = spWeb.Lists[“Tasks”];

SPList wflHistoryList = spWeb.Lists[“Workflow History”];

SPWorkflowAssociation wflAss = SPWorkflowAssociation.CreateListAssociation(wflTemplate, “NameOfTheWorkflow”, wflTaskList, wflHistoryList);

wflAss.AutoStartChange = false;

wflAss.AutoStartCreate = false;

wflAss.AllowManual = true;

spWeb.AllowUnsafeUpdates = true;

assocList.AddWorkflowAssociation(wflAss);

assocList.Update();

spWeb.AllowUnsafeUpdates = false;

}

Comments (1)

SharePoint: Workflow Migration

This is the second post related to SharePoint workflows. In first post, we have looked at the implementation details of the OOTB and SPD workflows. In the this post, we will look at the migration of OOTB and SPD 2010 workflows from one list to other or from one form to other.

Migration of Workflows: In order to migrate workflow from one site to other or from one server to other, we need to take care of three or four files mentioned previously. These files contain hard coded list and site GUID along with other details.

In case of OOTB workflow, instance details are stored in content database and can be seen in the table of “WorkflowAssociation”. Column “InstantiationParams” contains the details of users, order, due date etc. in XML format while other columns have of site / web / list IDs, name / description of workflow, task / history list IDs and names.

In case of SPD workflows, the definitions of workflow files are stored in list “workflows” created at the root of each site and web. In case of SPD 2007, files generated for a workflow are xoml, xoml.wfconfig.xml, aspx and xoml.rules if rules are used in the workflow.

Moving back to our original topic of migration, we need to change the GUID / ID of lists that are coded in xoml.wfconfig.xml file and you are done. A lot of third party workflow migrators are available but if you are interested in an open source solution, codeplex gives you hope.

http://spwflmigrator.codeplex.com/

This is a good resource for starting. Thanks rane_peinl J

In case of SPD 2010 workflows, files generated for a workflow are xoml, xoml.wfconfig.xml, xsn and xoml.rules if rules are used in the workflow. The big difference is the implementation of initiation form. In SPD 2007 workflow, initiation form is aspx page while in case of SPD 2010, initiation form is an InfoPath form. If you export the InfoPath form, add .cab with the form name and extract it, you will be able to find files that are generated as part of InfoPath form.

When we tried to migrate SPD 2010 workflow from one list to other, we were able to read xoml.wfconfig.xml and replace all the related IDs with the destination but we are unable to create a working InfoPath form. Although InfoPath is migrated successfully but when we try to start workflow, “Form has some error” message appeared. Creating a working initiation form for the migrated workflow doesn’t seem to be happening.

We do have third party solution that claims to migrate SPD 2010 workflow but I am waiting for their support response.

Comments (1)

SharePoint: Workflows Implementation

In this article we have tried to explore implementation details of workflows in SharePoint 2007 and 2010. SharePoint provides us with three flavors of workflows, namely, Out Of the Box (OOTB), SharePoint Designer (SPD) and Visual Studio workflows. We will discuss implementation details of OOTB and SPD workflow w.r.t. to SharePoint 2007 (MOSS) and SharePoint 2010.

Out Of the Box Workflows: OOTB workflows are workflows which come with SharePoint server installation.

In MOSS, these workflows come in rigid form. You can’t customize them. The only option available is to create instance of OOTB workflows in different list or libraries and run them as they are. The instance details of OOTB workflows are stored in content database.

In SharePoint 2010, OOTB workflows are also termed as “Globally Reusable Workflows”. Other than “Three State workflow” which comes with SharePoint Foundations (free of cost), all other workflows comes with server or enterprise licenses. Great news is that these workflows can be customized using SPD 2010. The definition of OOTB workflows can be found using SPD 2010. Open site in SPD 2010 and select “All Files”. In the right pane, select “_catalogs” and then “wfpub”, you will be able to find OOTB workflows.

When you customize “Globally Reusable Workflow”, the definition of newly created workflow is stored in a list “workflows”.

SharePoint Designer Workflows: Workflows created using SPD are termed as SPD workflows. They are also called declarative workflows because they consist of Extensible Markup Language (XOML) rather than compile code as in the case of Visual Studio workflows.

In SPD 2007, we have only one type of workflow, namely, List workflow. In case of SPD 2010, we have three different types of workflows, namely, List, Reusable and Site workflows. When we create a new SPD workflow, a list “workflows” is created if don’t exist. Workflows list is not visible in SharePoint UI and can be accessed via SPD or SharePoint Object Model (OM). We can traverse workflows list using OM as under:

using (SPSite spSite = new SPSite(“Site_URL”))

{

SPWeb spWeb = spSite.OpenWeb();

SPList spList = spWeb.Lists[“workflows”];

foreach (SPListItem spListItem in spList.Items)

{

Console.WriteLine(spListItem.Name);

}

}

In case of SPD, open site in SPD and select “All Files”. In the right pane, select “workflows” and you will be able to find SPD workflows

Workflows list contains all the files related to a particular workflow. For each SPD workflow, three or four files are created depending upon the existence rules or conditions.

  • xoml – Contains all the activities included in the workflow in Extensible Application Markup Language (XAML) format
  • xoml.wfconfig.xml – Contains details of the list or site GUIDs and start configuration of the workflow
  • xsn or aspx – In MOSS, an aspx page is created for starting workflow. In SharePoint 2010, we have InfoPath form in place of aspx page
  • xoml.rules – Contains workflow rules if used

In the next post we will discuss the issues relating to migration of SPD workflows.

Comments (1)