Sunday, February 1, 2015

AgilePoint InfoPath C# snippet - Retrieve repeating row data

Sometimes we would need to manipulate data from an InfoPath repeating field, use this data in our AgilePoint workflows. We would need to add custom code for this by using the Advanced Extensions - Managed Code stencil.

Sample of InfoPath schema:



I have a repeating row called Item with 3 fields or nodes.

My example would be to invoke a subprocess for each row of that data.
In order to access the data, I would need the System.Xml and System.Xml.XPath references in my code. I will make use of the XPathNavigator and XPathIterator functions to get data.



Tip:
You could use the following lines to retrieve the entire InfoPath xml document.
                WFCustomAttributes ds = api.GetCustomAttrs(pi, false);  
                XmlDocument doc = ds.GetXmlDocument();  

Here is the entire block of code.

 using System;  
 using System.Collections;  
 using System.Data;  
 using System.Xml;  
 using System.Xml.XPath;  
 using System.Net;  
 using System.IO;  
 using System.Text;  
 using System.Linq;  
 using Ascentn.Workflow.Base;  
 namespace ManagedCode  
 {  
      public class CSharpCodeSnippet  
      {  
           // Invoke method is this class's entry point  
           public void Invoke(  
                WFProcessInstance pi,   
                WFAutomaticWorkItem w,   
                IWFAPI api,   
                NameValue[] parameters)  
           {  
                WFCustomAttributes ds = api.GetCustomAttrs(pi, false);  
                XmlDocument doc = ds.GetXmlDocument();  
                XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);   
                mgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-01-27T23:22:27");  
                XPathNavigator nav = doc.CreateNavigator();  
                XPathNodeIterator nodes = nav.Select("/my:myFields/my:Items/my:Item", mgr);  
                string test = api.GetCustomAttr(pi.WorkObjectID, "/my:myFields/my:Items/my:Item").ToString();  
                foreach (XPathNavigator item in nodes)  
                {  
                     // Get Items  
                     XPathNavigator i = item.SelectSingleNode("my:ItemNo", mgr);  
                     string item = i == null ? string.Empty : i.Value;  
                     i = item.SelectSingleNode("my:ItemDescription", mgr);  
                     string description = i == null ? string.Empty : i.Value;  
                     Logger.WriteLine("Item found: " + item);  
                     try  
                     {  
                          NameValue[] attrs = NameValue.Array(  
                               "ItemNo", item,  
                               "ItemDescription", description  
                          );  
                          TriggerSubProcess(pi, w, api, attrs, fileID);  
                     }  
                     catch (Exception e)  
                     {  
                          Logger.WriteLine("Error while calling subprocess: " + e.ToString());  
                     }  
                }  
           }  
           private void TriggerSubProcess(WFProcessInstance pi, WFAutomaticWorkItem w, IWFAPI api, NameValue[] attrs, string fileID)  
           {  
                string subprocess = "EForm - Update Item Permission";  
                //Get UUID for the released process definition  
                string pid = api.GetReleasedPID(subprocess);  
                //Generate a unique process instance ID  
                string piid = Ascentn.Workflow.Base.UUID.GetID();  
                //Generate a unique name for the process instance  
                string piname = "UpdateItemPermission_" + fileID + "_" + DateTime.Now.ToString("MMdd-HHmmss");  
                //Generate a unique work object ID  
                string workObjectId = string.Format("MyProcess-{0}", UUID.GetID());  
                //Process ID of the parent  
                string superPIID = w.ProcInstID;  
                // Start the process  
                WFEvent evt = api.CreateSubProcInst(api.GetProcInst(w.ProcInstID), api.GetActivityInst(w.ActivityInstID), false, pid, piid, piname, workObjectId, "", superPIID, "", "", attrs, true);  
                //WFEvent evt = api.CreateSubProcInst(api.GetProcInst(w.ProcInstID), api.GetActivityInst(w.ActivityInstID), false, pid, piid, piname, workObjectId, true);  
                Logger.WriteLine("Subprocess triggered to update item permission.");  
           }  
      }  
 }  

No comments:

Related Posts Plugin for WordPress, Blogger...