in SharePoint 2010

SharePoint 2010:Starting Site Workflows Programmatically

So you have created a very cool site workflow in SharePoint Designer 2010, published and you want to start programmatically.

 

Below is the code snippet to start a site workflow. Credit goes to MSDN user Paul Turner :

 

static void Main(string[] args)
{
    using (SPSite spSite = new SPSite("http://mysite"))
    {
        using (SPWeb spWeb = spSite.RootWeb)
        {
            SPWorkflowAssociation workflowAssociation

                                                 = spWeb

                                                   .WorkflowAssociations

                                                   .GetAssociationByName("MySiteWorkflow",

                                                                          CultureInfo.InvariantCulture);
            if (workflowAssociation != null)
            {
                SPWorkflow siteWorkflow

                                  = spSite

                                    .WorkflowManager

                                    .StartWorkflow(spSite,

                                    workflowAssociation,

                                    "<Data><SalesOrderId>71774</SalesOrderId></Data>",

                                   SPWorkflowRunOptions.Asynchronous);
            }
        }
    }

    Console.WriteLine("Workflow Started");

    Console.Read();
}

 

The <Data><SalesOrderId>71774</SalesOrderId></Data> is the initiation form data. Replace SalesOrderId with yours.

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.