Initiate Process From ASP.NET form?

  • 27 September 2007
  • 12 replies
  • 4 views

Badge +5

Hi,

 I'm stuck in this part, the process flow seems to be OK, I can instantiate the process from the BP workspace. But I can't do so in the ASP.NET form. What is the best way to instantiate a process from an ASP.NET web form?

 

Cheers,

Saleh 


12 replies

Badge +5

Hi Saleh,


To simply instantiate a process using an ASP webform you will have to make use of SourceCode.Workflow.Client. You will have to first add a reference to SourceCode.Workflow.Client and a very simple example of the code is :


            //Create your connection object
            Connection K2Con = new Connection();
            //Open a connection to the K2[blackpearl] server
            K2Con.Open("bpserver");
            //Create the processInstance using the Project and Process name.
            ProcessInstance pi = K2Con.CreateProcessInstance(@"ProjectProcess");
            //Set the required datafields
            pi.DataFields["DF1"].Value = "SomeValue";
            //update the process instance
            pi.Update();
            //start the process
            K2Con.StartProcessInstance(pi);
            //close the connection
            K2Con.Close();


Please note that when using a Webform that "identity impersonate" is set to true in the web.config.


 You can have a look at all the different classes, methods and their usages of "SourceCode.Workflow.Client" in the "UserGuide > References" section of the Documentation.


Note that processes can also be started using smartobjects through ASP and this is explained in the tutorials section of the Documentation.


Let me know if you have any other questions


Gert

Badge +5

Hi Gert,

That's very informative, it's exactly what I need. Especially, for the reference, I don't know why I didn't check it before... Thanks!

Btw, Do I have to change the action "submit button" for each activity "Web Form".

 

Regards,

Saleh 

Badge +5

HI,


Not quite sure if I understand you correctly.


To action a worklist item you will have to open the worklist item using the serial number passed, retrieve/set required datafields, update the "WorklistItem.ProcessInstance" and execute the desired action. e.g.


//Create,Open Connection K2Connection first
//Open WorklistItem
WorklistItem wli = K2Con.OpenWorklistItem(Request.QueryString["SN"], "asp");
//Update Fields etc
//Execute Required action
wli.Actions["Approve"].Execute();


Again note that this is a VERY basic example.


Hope this helps


Gert

Badge +5

I meant is the initiation code enough for all web forms within a process. One other words, for each step "Activity/WebForm" the developer has to specify a certain code to action an item in a worklist "for example, Approve/Reject".

In K2.NET 2003, It was very straightforward to fulfill a process using web forms "ASPX", you can use the K2 Web Controls and they will handle the action process internally, there is no code needed to accomplish such a simple and common task.

Badge +5

Unfortunately smartforms are not available in K2[blackpearl] and K2.Net 2003 Smartforms were also not available in VisualStudio 2005.


 There are two "easy" ways to action a worklist item.



  1. Enable the client event for batch actioning, which will then enable it to be actioned from the workspace.
  2. You could also make use of the "Forms Generation Client" event which will generate a webform for your actioner to complete using the default blank template or a custom designed one. You can create a new template by making a copy of the "Blank Page" folder at "C:Program FilesK2 BlackpearlBinLayoutsclientevent CSharp1033" and give it a different name. Modify the the template to suite your needs. Note that the keywords pre- and succeeded with '$' should not be modified.

Other than these two you will have to action a worklist item by writing your own code using  "SourceCode.Workflow.Client".


Regards


Gert

Badge +2

Hi,


i just wanna ask. i'm still confused with this asp web form. i'm using k2 workflow in this form.


i put a submit button in the form so that the user can submit it and start the work flow ( to the the approver).


but the problem is the when the user click the submit button, it doesnt continue the workflow but record all the data. it still stuck at the applicant level.


what should i do to make the button works?

Badge +8

Post your code in order for us to have a look. 

Badge +2

Owh.I'm sorry.Here is my code:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SourceCode.Workflow.Client;


namespace FamilyDayVB
{
    public partial class _Default : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
        }


        protected void Submit_Click(object sender, EventArgs e)
        {
            //Declare new K2 Connection & Process Instance objects
            Connection conn = new Connection();


           //Connect to Workflow server
            conn.Open("Myserver");


            //Create process instance
            string name = @"FamilyDayFamilyDay";
            ProcessInstance pi = conn.CreateProcessInstance(name);


            //Map the data to the K2 process data fields
            pi.DataFields["StaffID"].Value = txtStaffID.Text.Trim();
            pi.DataFields["Name"].Value = txtName.Text.Trim();
            pi.DataFields["Division"].Value = ddlDivision.SelectedValue.Trim();
            pi.DataFields["Department"].Value = ddlDept.SelectedValue.Trim();
            pi.DataFields["Voucher"].Value = ddlVouchar.SelectedValue.Trim();
            pi.DataFields["Size"].Value = ddlSize.SelectedValue.Trim();
            pi.DataFields["Marital"].Value = rbtMarital.SelectedValue.Trim();
            pi.DataFields["Attendance"].Value = rbtAttendance.SelectedValue.Trim();


            //Set the folio
            DateTime Date = DateTime.Now;
            pi.Folio = "FDR" + Date + txtStaffID.Text.Trim();
            pi.DataFields["RefNo"].Value = pi.Folio;


            //Start K2 process instance
            conn.StartProcessInstance(pi);


            //display status msg to user
            lblStatus.Text = "Your new process number is : " + pi.ID;


            //Close connection
            conn.Close();
        }


      }
}

Badge +8

The code posted seems correct, it should start a process if there's no exception.


But I have a question on your previous post, you stated that the Workflow records the data but it doesn't continue. The data wont get saved if the process doesn't start. If you are struggling with a Client Event's web page, this code won't work. 


As a reference, have a look at the following example in blackmarket, it should give you better insight on the different options when coding against the API:


http://www.k2underground.com/groups/k2_blackpearl_ajax_aspnet_api_example/default.aspx

Badge +2

Dear DC,


Thanx for the references. =)


Yes.I'm using Client Event's web page on the workflow and check the web page check box and provide the web page URL as example


http://localhost:4057/FDSubmit.aspx?. Why this code won't work on the client event and what should i use instead of it.


Please do help me.I'm really confused now..

Badge +8

The code that you posted above will start a new process instance, it does not interact with an existing one. To action an existing Worklist item, you will need to call the OpenWorklistItem method of the Connection object. 


You can search for its usage in the example or have a look at the K2 Help file, the Developer reference section will also have some sample usage.

Badge +2

Thanx DC!


Really appreciate it! ;D

Reply