Accessing expressionbox information in code. - InfoPath Dev
in

InfoPath Dev

Accessing expressionbox information in code.

Last post 02-28-2009 02:10 PM by Patrick Halstead. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 06-22-2007 02:28 AM

    Accessing expressionbox information in code.

    Hello,

     Is it possible to retrieve the text entered in an expression box in the forms code?

     

    Thanks in advance

    Hans.
    Infopath 2003 / Jscript
  • 06-22-2007 05:45 AM In reply to

    Re: Accessing expressionbox information in code.

    The short answer is "no" - the expression box text is not stored in the data and you can only manipulate the data in the forms code.

    The long answer is "well, it depends". There are two possible things you can put into an expression box:

    - an xpath xpression (as complex as you want)

    - plain text

    In the first case, you are have to run this xpath yourself in the proper context in your business logic (for example, if an expression box is summing up the columns of a particular row, get the xml node for that row and execute that xpath).

    In the later case, when you have just typed it yourself, the value lives in the view.xsl. Then to retrieve it you can use the following approach: load up the view into an xml document, locate the span for the expression box and get the value out of it:

    var domObject = XDocument.CreateDOM();
    domObject.load("view1.xsl");
    domObject.setProperty("SelectionNamespaces", 'xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"');
    var nodeObject = domObject.selectSingleNode("//span[@xd:CtrlId='CTRL1']/xsl:value-of/@select");
    XDocument.UI.Alert(nodeObject.text.substring(1, nodeObject.text.length - 1));

    However, it's too much work for something that is very simple (it's easier to just re-type the string in the code) so I am assuming you are really talking about the first scenario. In that case, once again, you really have to re-execute the xpath manually because opening the view xsl won't help you - the value is dynamic and the runtime view cannot be accessed in code.

    David Airapetyan / Software Services Architect
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 06-22-2007 07:13 AM In reply to

    Re: Accessing expressionbox information in code.

    Thanks for the response.

    However I still have difficulties to access all data via code.

    now I Retrieve the information into the expressionbox with the following statement.

    xdXDocument:GetDOM("Approvers")/dfs:myFields/dfs:dataFields/d:LOCALSALES_APPROVERS/@EmployeeEmail[../@EmployeeID = current()/@Approved_by1]

    Can someboy help me on how I get this into a variable?

     

    Hans.
    Infopath 2003 / Jscript
  • 06-22-2007 07:43 AM In reply to

    Re: Accessing expressionbox information in code.

    Yes, the only thing that won't work in your code is xdXDocument:GetDOM() because you don't have an XSLT processor where this function is defined.

    However, there is an equivalent you can use in code:

    XDocument.GetDOM(domName)

    So your code then becomes this:

    XDocument.GetDOM("Approvers").selectSingleNode("/dfs:myFields/dfs:dataFields/d:LOCALSALES_APPROVERS/@EmployeeEmail[../@EmployeeID = current()/@Approved_by1]")

    ("Approvers").selectSingleNode("/dfs:myFields/dfs:dataFields/d:LOCALSALES_APPROVERS/@EmployeeEmail[../@EmployeeID = current()/@Approved_by1]")

    David Airapetyan / Software Services Architect
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 06-24-2007 11:31 PM In reply to

    Re: Accessing expressionbox information in code.

    the code :  var sSearchString =XDocument.GetDOM("Approvers").selectSingleNode("/dfs:myFields/dfs:dataFields/d:LOCALSALES_APPROVERS/@EmployeeEmail[../@EmployeeID = current()/@Approved_by1]")

    Returns the following error: Reference to undeclared namespace prefix: 'dfs'

     

    What am I doing wrong?

     

    Hans.
    Infopath 2003 / Jscript
  • 06-25-2007 12:54 AM In reply to

    Re: Accessing expressionbox information in code.

    What I bassicly want to do is retrieve an email address (field EmployeeEmail) from a secondary datasource based on an EmployeeID (Field EmployeeID in table LOCALSALES_APPROVERS). The EmployeeID is entered as Approved_by1in the table LOCALSALES (=main data source).

    Is there an easy understandable solution that a newbie like me can reproduce somewhere?

    All my tables are in a SQL 2000 Database.

     

     

    Hans.
    Infopath 2003 / Jscript
  • 06-25-2007 03:29 AM In reply to

    Re: Accessing expressionbox information in code.

    Since there came no answers, I started looking for the answers myself. And eventualy I found the solution. It was a combination of Greg Collins http://www.infopathdev.com/files/folders/examples/entry283.aspx and Matt Faus http://www.infopathdev.com/blogs/matt/archive/2006/02/02/Setup-Namespaces-for-a-Secondary-Data-Source.aspx that did the job. I would like to thank both of you for the provided help. For what it's worth, this is the code

    /*
     * This file contains functions for data validation and form-level events.
     * Because the functions are referenced in the form definition (.xsf) file,
     * it is recommended that you do not modify the name of the function,
     * or the name and number of arguments.
     *
    */

    // The following line is created by Microsoft Office InfoPath to define the prefixes
    // for all the known namespaces in the main XML data file.
    // Any modification to the form files made outside of InfoPath
    // will not be automatically updated.
    // Added namespace according to Matt's article
    //<namespacesDefinition>
    XDocument.DOM.setProperty("SelectionNamespaces",'xmlns:q="http://schemas.microsoft.com/office/infopath/2003/ado/queryFields" xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-06-19T11:16:40"');
    XDocument.GetDOM("Approvers").setProperty("SelectionNamespaces",'xmlns:ns1="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:d1="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"')
    //</namespacesDefinition>

    // This function is added out of Greg's example
    // To tell the truth: I don't really know where this is about.
    // String object extension. Usage: stringObj.FunctionName([parameters]);
    // Example usage: "This is a {0}".Format("test."); returns "This is a test.";
    String.prototype.Format = new Function("var arg = String.prototype.Format.arguments; var str=this.toString(); var ph,re; for(var i=0;i<arg.length;i++){ph=new RegExp('\{('+i+')\}','ig'); if(null==arg[i]) re=''; else re=arg[i].toString(); str=str.replace(ph,re);} return str;");


    //=======
    // The following function handler is created by Microsoft Office InfoPath.
    // Do not modify the name of the function, or the name and number of arguments.
    // This function is associated with the following field or group (XPath): /dfs:myFields/my:Department
    // Note: Information in this comment is not updated after the function handler is created.
    //=======
    function msoxd_my_Department::OnAfterChange(eventObj)
    {
    // Write code here to restore the global state.

     if (eventObj.IsUndoRedo)
      {
       // An undo or redo operation has occurred and the DOM is read-only.
       return;
      }

     // Retrieve department info into sSearchstring
     var sSearchString = trim(XDocument.DOM.text);
     // setup connection for EmployeeInfo
     var oAdoAdapter = XDocument.DataObjects("EmployeeInfo").QueryAdapter;
     //  construct select
     var sCommand = "select EmployeeID, Lastname + ' ' +Firstname + ' (' + EmployeeID + ')' as employeeinfo,orgunitdescr from dbo.EMPLOYEE where OrgUnitDescr = '" + sSearchString +"' and plantaa='AA' order by employeeinfo";
     // apply appropriate select
     oAdoAdapter.Command = sCommand;
     // Requery the datasource
     oAdoAdapter.Query(); 
    // XDocument.UI.Alert("Please stop")


    }


    function trim(string) {
      // retourneert de getrimde string (string ontdaan van leading en trailing whitespaces/tabs/newlines)
      var remove = " \t\r\n"; // te verwijderen karakters
      var ret = ""; // return string
      var start = 0; // startpositie in de te controleren string
      var end = string.length - 1; // eindpositie in de te controleren string

      // schuif de startpositie op
      while(start <= end && remove.indexOf(string.charAt(start)) != -1) {
        start++;
      }

      // schuif de eindpositie op
      while(end >= start && remove.indexOf(string.charAt(end)) != -1) {
        end--;
      }
     
      // als string leeg is, geldt (end + 1 == start)

      // bouw de overgebleven string
      for(i=start; i <= end; i++) {
        ret += string.charAt(i);
      }
     
      return ret;
    }

    //=======
    // The following function handler is created by Microsoft Office InfoPath.
    // Do not modify the name of the function, or the name and number of arguments.
    // This function is associated with the following field or group (XPath): /dfs:myFields/dfs:dataFields/d:LOCALSALES/@Approved_by1
    // Note: Information in this comment is not updated after the function handler is created.
    //=======
    function msoxd__LOCALSALES_Approved_by1_attr::OnAfterChange(eventObj)
    {

     // Retrieve department info into sSearchstring
     // I suppose eventObj.NewValue gets the information just entered.
     var sName = eventObj.NewValue;
     // Another 2 lines that are a mistery to me
     var oRow = eventObj.Site.selectSingleNode("..");
     var oSqlCustomer = XDocument.GetDOM("Approvers").selectSingleNode("//d1:LOCALSALES_APPROVERS[@EmployeeID  = '{0}']".Format(sName));
     var sSearchString = oSqlCustomer.getAttribute("EmployeeEmail")
     // But the correct information shows up
     XDocument.UI.Alert(sSearchString)


     if (eventObj.IsUndoRedo)
      {
       // An undo or redo operation has occurred and the DOM is read-only.
       return;
      }

    }

    Also thanks to davidair for the initial answers.

    Hans.
    Infopath 2003 / Jscript
  • 02-27-2009 12:44 PM In reply to

    Re: Accessing expressionbox information in code.

    Is it possible to put something into this expression box rather than retrieve it?

    Assume that I have something like the following:

    <Name>
      <First>Bob</First>
      <Last>Smith</Last>
    </Name>

    My goal is to create a display-only node with no binding, in this case I'd like to display "Smith, Bob", or something to that effect.  An expression box makes the most sense, but I've also looked at trying to us an unbound textbox.  Unlike the example the xpath expression I'd have to write is far too complicated and it is something I should be able to do rather easily programatically, but are you saying there is no way to access this at runtime?

  • 02-27-2009 04:28 PM In reply to

    Re: Accessing expressionbox information in code.

    I came up with an interesting solution using a secondary data source.  Essentially I created an XML file that was only used for display reasons.  I set it up as a receiving source and then oddly enough, I write values to that node.  This has bad results when used with a repeating table or section.  I could use some sort of keying mechanisim to bind the two sources, but if you might have node structures reordering, it would be easy to get those out of sync.

    I believe the way I'm going to address this is to add a display node to the repeating section and write to that.  So as not to include that information when I write the data to disc, I will modify the tree on save to prune those nodes.  Not ideal, but I think it will work.

  • 02-28-2009 02:10 PM In reply to

    Re: Accessing expressionbox information in code.

    Hey Ryan,

    Glad to hear you've discovered secondary data sources (2DSes). We love these for a variety of reasons (most because they're fast and useful for meta data like guidance text, workflow status, etc.). They do pose some limitations (eventing, validation, etc.). Check out our free qRules download for commands that help you copy items from your 2DS to a main data source. Might help you keep them in synch. But, sounds like your approach of ID'ing them is also a good one.

    Patrick Halstead
    Project Manager at Qdabra
Page 1 of 1 (10 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.