Freigeben über


How to start a workflow programmatically in the InfoPath code using Workflow web service

How to start an Approval workflow programmatically in the InfoPath form code?

  1. Create a web reference that points to https://yourSiteUrl/_vti_bin/workflow.asmx .
  2. Configure an Approval workflow in your form library and capture the template id.

From form library settings->workflow settings, click the workflow name, right click in the page to View Source. Search for “templateid”. The TemplateID looks like the following:

TemplateID=%7b5C65AB69-11BC-430D-B730-71050250F576%7d

(“%7b” is encoded for “{“ and “%7d” is encoded for “}”)

GUID for the template id is : 5C65AB69-11BC-430D-B730-71050250F576

  1. Write the InfoPath code. The highlighted parts in the sample code below needs to be replaced. 

**TIP: If you can't get the workflowParamers XML string right, you can write a simple console app in server using OM. SPWorkflowAssociation.AssociationData will return the init form data that you already configured in the list/library workflow settings.

 

public void submit_OnClick(DocActionEvent e)

        {

            // Write your code here.

            WorkflowService.Workflow workflowService = new WorkflowService.Workflow();

            workflowService.Url = "https://siteURL/_vti_bin/workflow.asmx";

            workflowService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            workflowService.PreAuthenticate = true;

            try

      {

                XmlDocument workflowParameters = new XmlDocument();

                string strXml = "<my:myFields xml:lang='en-us' xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance' xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD'><my:Reviewers><my:Person><my:DisplayName>Approver’s display name</my:DisplayName><my:AccountId>domain\\account</my:AccountId><my:AccountType>User</my:AccountType></my:Person></my:Reviewers><my:CC></my:CC><my:DueDate xsi:nil='true'></my:DueDate><my:Description></my:Description><my:Title></my:Title><my:DefaultTaskType>1</my:DefaultTaskType><my:CreateTasksInSerial xsi:nil='true'></my:CreateTasksInSerial><my:AllowDelegation>true</my:AllowDelegation><my:AllowChangeRequests>true</my:AllowChangeRequests><my:StopOnAnyReject xsi:nil='true'></my:StopOnAnyReject><my:WantedTasks xsi:nil='true'></my:WantedTasks><my:SetMetadataOnSuccess xsi:nil='true'></my:SetMetadataOnSuccess><my:MetadataSuccessField></my:MetadataSuccessField><my:MetadataSuccessValue></my:MetadataSuccessValue><my:ApproveWhenComplete xsi:nil='false'></my:ApproveWhenComplete><my:TimePerTaskVal xsi:nil='true'></my:TimePerTaskVal><my:TimePerTaskType xsi:nil='true'></my:TimePerTaskType><my:Voting>false</my:Voting><my:MetadataTriggerField></my:MetadataTriggerField><my:MetadataTriggerValue></my:MetadataTriggerValue><my:InitLock xsi:nil='false'></my:InitLock><my:MetadataStop xsi:nil='true'></my:MetadataStop><my:ItemChangeStop xsi:nil='true'></my:ItemChangeStop><my:GroupTasks>false</my:GroupTasks></my:myFields>";

                workflowParameters.LoadXml(strXml);

//Guid for the workflow templateId

Guid guid = new Guid("{5C65AB69-11BC-430D-B730-71050250F576}");

//form URL needs to be replaced

XmlNode result = workflowService.StartWorkflow("https://siteURL/formlibrary/test.xml", guid, workflowParameters);

                if (result != null)

                    thisXDocument.UI.Alert("Result: " + result.Value);

               

            }

            catch (SoapException ex)

            {

       thisXDocument.UI.Alert("SoapException: " + ex.Message + " " + ex.Detail.Value);

            }

            catch (Exception ex0)

            {

                thisXDocument.UI.Alert("Exception: " + ex0.Message);

            }

           

        }

Comments

  • Anonymous
    January 17, 2008
    PingBack from http://stevepietrekweblog.wordpress.com/2008/01/17/links-1172008/

  • Anonymous
    January 29, 2008
    Hi Jingmei, I've been indeed trying to exactly do what the title of this post says but without much success so far. This seems like a reasonable solution but I haven't managed to get it done because of a couple of issues:

  • I can't find an equivalent of your "http://yourSiteUrl/_vti_bin/workflow.asmx" path, as a matter of fact there is not even a "_vti_bin" folder in my current MOSS installation. Should I somehow create a virtual url that points to the workflow itself?
  • Where is the "WorkflowService.Workflow" class coming from? How can I add it to my VSTO dev. environment? Any help and/or ideas would be much appreciated, cheers Jaume
  • Anonymous
    January 29, 2008
    Hi Jaume: Refer to http://msdn2.microsoft.com/en-us/library/websvcworkflow.aspx on the Workflow web service. In your Visual Studio project, you can add a web reference and point to the http://yourSiteUrl/_vti_bin/workflow.asmx" path. If you have MOSS installed properly you should be able to browse to it. Jingmei

  • Anonymous
    February 05, 2008
    Indeed the http://yourSiteUrl/_vti_bin/workflow.asmx resolves and the webservice can be accessed and added to VS as a web reference. However, VS still does not validate the WorkflowService class and fails to compile. I have added some references to some apparently related libraries like System.WorkflowServices, but without much luck. Could you please post your "using" directives? Any further suggestions? thanks again

  • Anonymous
    February 05, 2008
    I've just noticed that WorkflowService  is the actual reference to the http://yourSiteUrl/_vti_bin/workflow.asmx web service. Anyway I would say that this makes my last question pretty redundant. It's all compiled and sorted out, thanks!