Skip to main content

This is a SmartObject centric way of creating a record in a SmartObject and creating a process that can relate to the SO based upon the SmartObject's ID.


Similar code to this was used with SmartBox based SmartObjects and K2 blackpearl] RTM.


(There are several places you have to update if you change the name of your SmartObject these are noted with //**)
(Fields display here behind the @ symbols in the parameter statements reflect properties in the SmartObjects - they will need to be customized per your SmartObject Properties also and are noted with //***)
(This SmartObject had many fields, I haven't left them all in..)


protected void btnStartProcess_Click(object sender, EventArgs e)
{
        //for process instantiation
        string m_strBPServer = "blackpearl";
        string m_strProcessName = "ProjectProcess"
        //for smartobject instantiation 
        string m_strSOConnTasks = "Server=blackpearl;Port=5555;AutoAlias=False;SmartObjects=Tasks";//**

//BEGIN  TASKS SmartObject
            string strSelectCmd = "Tasks.Create";//**

            SourceCode.Data.SmartObjectsClient.SOConnection oConn = new SourceCode.Data.SmartObjectsClient.SOConnection(m_strSOConnTasks);//**

            SourceCode.Data.SmartObjectsClient.SOCommand oCmd = new SourceCode.Data.SmartObjectsClient.SOCommand(strSelectCmd, oConn);
            SourceCode.Data.SmartObjectsClient.SODataReader oRdr = null;


            oCmd.CommandType = CommandType.StoredProcedure;



           
            //Task Name
            SourceCode.Data.SmartObjectsClient.SOParameter oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskTitle", txtTaskerName.Text);//***
            oCmd.Parameters.Add(oParam);


            //customer
            oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskCustomer", txtCustomer.Text);//***
            oCmd.Parameters.Add(oParam);


            //Originator from impersonated user
            oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskOriginator", HttpContext.Current.User.Identity.Name.ToString());//***
            oCmd.Parameters.Add(oParam);


            //Origination date from now()
            oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskOriginationDate", DateTime.Now);//***
            oCmd.Parameters.Add(oParam);


            //Status ****** hardcoded value *****
            oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskStatus", "Created");//***
            oCmd.Parameters.Add(oParam);


            //ExternalSuspenseDate
            oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskExternalSuspenseDate", Convert.ToDateTime(txtcalExternalSuspenseDate.Text));//***
            oCmd.Parameters.Add(oParam);


            //Notes
            oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskNotes", txtNotes.Text);//***
            oCmd.Parameters.Add(oParam);


            //Order  
            oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskOrder", ddlOrder.SelectedValue);//***
            oCmd.Parameters.Add(oParam);


 


            if (FileUpload1.HasFile || FileUpload2.HasFile){
                m_blHasDocument = true;
            }


            //HasDocument
            oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@tskHasDocument", m_blHasDocument);
            oCmd.Parameters.Add(oParam);



            // Write Task Smobject - get ID back -- if you don't want to read use "ExecuteNonQuery" instead
            oRdr = oCmd.ExecuteReader(CommandBehavior.CloseConnection);


            if (oRdr != null)
            {
                while (oRdr.Read())
                {
                    // note the names in the oRdr collection correspond to SmartObject properties
                    // i.e. oRdrÂ"FirstName"] means there is a SmartObject property called "FirstName"
                    m_intTaskSOID = Convert.ToInt32(oRdrÂ"tskID"].ToString());
                    //lblLastName.Text = oRdrb"LastName"].ToString();
 
                }
            }
            oRdr.Close();
//END TASKS SmartObject call


//BEGIN PROCESS
            //Instantiate the Connection Object
            SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
            //Open the Connection Object
            K2Conn.Open(m_strBPServer);


            //Declare the Process Instance Object
            SourceCode.Workflow.Client.ProcessInstance K2ProcInstance;
            //Create a Proc Instance
            K2ProcInstance = K2Conn.CreateProcessInstance(m_strProcessName);

            //Add process level data field to allow relating to the SmartObject
            K2ProcInstance.DataFields "TasksID"].Value = m_intTaskSOID.ToString();
           
            //customize the Folio
            K2ProcInstance.Folio = txtTaskerName.Text;


            //kick the process off
            K2Conn.StartProcessInstance(K2ProcInstance, false);
            K2Conn.Close();


//END OF PROCESS CODE   


//BEGIN ATTACHMENTS 
        //with the same SmartObject Id used to relate the Process we can relate other data
        //in this case:    m_intTaskSOID
        //Attachments into SmObjs
        //similar method to the one called here can be found here

            if (FileUpload1.HasFile)
            {
                string strFilePath = m_strAttachmentLocation + FileUpload1.FileName;
                FileUpload1.PostedFile.SaveAs(strFilePath);
                AttachmentsCreate(m_intTaskSOID, strFilePath, HttpContext.Current.User.Identity.Name.ToString(), DateTime.Now);
                System.IO.File.Delete(strFilePath);
            }
            if (FileUpload2.HasFile)
            {
                string strFilePath = m_strAttachmentLocation + FileUpload2.FileName;
                FileUpload2.PostedFile.SaveAs(strFilePath);
                AttachmentsCreate(m_intTaskSOID, strFilePath, HttpContext.Current.User.Identity.Name.ToString(), DateTime.Now);               
                System.IO.File.Delete(strFilePath);
            } 
//END ATTACHMENTS
            
        btnStartProcess.Enabled = false;
}

Be the first to reply!

Reply