Refresh InfoPath field after code-based calculation - InfoPath Dev
in

InfoPath Dev

Having trouble finding a blog or post that answers your question? Check out our Custom Search Page

Refresh InfoPath field after code-based calculation

Last post 08-17-2012 10:45 AM by Gennady Vanin (Геннадий Ванин, Новосибирск). 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 08-05-2012 10:33 PM

    Refresh InfoPath field after code-based calculation

    I have an InfoPath 2010 form that creates a new Sharepoint list item. The form has, in essence, one input field and one output field. If something gets entered in the input field then some calculation is performed and I want the result to show up in the output field before the user clicks "OK" or "Reject". As the calculation is somewhat complicated I need to do it in code, not through rules.

    I am doing this:

    private void CalculateOutput()
    {
        XPathNavigator main = MainDataSource.CreateNavigator();
        XPathNavigator service = main.SelectSingleNode("my:meineFelder/my:serviceValue", NamespaceManager);
        double serviceValue = service.ValueAsDouble;
        double output = 3*serviceValue;   // much abbreviated version
    
        // output now has the correct value
        XPathNavigator outputNav = main.SelectSingleNode("my:meineFelder/my:output", NamespaceManager);
    
        //Remove the "nil" attribute (otherwise we get an error when the field is a double instead of a string)
        if (outputNav.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))  outputNav.DeleteSelf();
    
        outputNav.SetValue(output.ToString());
     }
    

    My problem is that everything works fine in the InfoPath preview but once I deploy the form and run it in the browser the output field does not get updated, i.e. I cannot see the result of my calculations.

    I have tried .ReplaceSelf (with OuterXML), with zero result. I have also played with assigning the result to another field and thereby firing an "set field value" rule, also to no avail.

    If I submit the form, the output value is saved OK but I'd really like the user to see the output field before submitting. I guess I need a way to refresh the form but I can't see how.

    Any Ideas? Thx a lot.

  • 08-06-2012 07:33 AM In reply to

    Re: Refresh InfoPath field after code-based calculation

    bluewater:

    I have an InfoPath 2010 form that creates a new Sharepoint list item. The form has, in essence, one input field and one output field. If something gets entered in the input field then some calculation is performed and I want the result to show up in the output field before the user clicks "OK" or "Reject". As the calculation is somewhat complicated I need to do it in code, not through rules.

    I am doing this:

    private void CalculateOutput()
    {
        XPathNavigator main = MainDataSource.CreateNavigator();
        XPathNavigator service = main.SelectSingleNode("my:meineFelder/my:serviceValue", NamespaceManager);
        double serviceValue = service.ValueAsDouble;
        double output = 3*serviceValue;   // much abbreviated version
    
        // output now has the correct value
        XPathNavigator outputNav = main.SelectSingleNode("my:meineFelder/my:output", NamespaceManager);
    
        //Remove the "nil" attribute (otherwise we get an error when the field is a double instead of a string)
        if (outputNav.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))  outputNav.DeleteSelf();
    
        outputNav.SetValue(output.ToString());
     }
    

    My problem is that everything works fine in the InfoPath preview but once I deploy the form and run it in the browser the output field does not get updated, i.e. I cannot see the result of my calculations.

    I have tried .ReplaceSelf (with OuterXML), with zero result. I have also played with assigning the result to another field and thereby firing an "set field value" rule, also to no avail.

    If I submit the form, the output value is saved OK but I'd really like the user to see the output field before submitting. I guess I need a way to refresh the form but I can't see how.

    Any Ideas? Thx a lot.

    For me, it is unclear how to reproduce what you have written.

    It is impossible to attach a custom code to Sharepont List Form (as it appears on compatibility < Form Options) or submit it from Filler/Preview while running it from browser doesn't have Submit but only Save

    It is possible to have  Submit and custom code for forms in document (or form) library but then it is not clear from your descriptions how you managed "everything works fine in the InfoPath preview" to create new sharepoint list items

    Probably you missed or named wrongly some terms in your descriptions

    Can you attach your form template (in options tab of a post) and/or describe how to reproduce what you have done?

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 08-07-2012 10:58 AM In reply to

    Re: Refresh InfoPath field after code-based calculation

    Gennady,

     thank you for looking into this. You are quite right, of course - I meant to say a forms library, not a list. Sorry, SP newbie ...

    Also, I have hidden the form's default toolbar (that includes the submit button) and have replaced it with a button of my own that sits on the form and does a "send to datasource".

    I am away from the office for the next two days but will post the form template as soon as I get back. Until then, thx again.

    bluewater

  • 08-17-2012 02:22 AM In reply to

    Re: Refresh InfoPath field after code-based calculation

    Sorry I took so long to reply, but I had the brilliant idea to dcpromo my VM machine. Friggin' nightmare....  took a long time to get everything back to working but it's OK now...

     Back to my original problem (field computed in infopath code does not get refreshed in browser). I am attaching the infopath form. I have also done a little video that demonstrates the problem: http://www.screencast.com/t/4SCAGhGn

     thank you for looking into this.

  • 08-17-2012 04:47 AM In reply to

    Well, it cannot be viewed much... 

    The VSTA project code is absent, i.e. on pressing Code Editor, I am getting:

    •   Infopath cannot start Microsoft Visual Studio Tools for Applications
      The Visual Basic or C# project for this form template cannot be found
      To locate the project yourself, click Browse
      Browse
      Cancel

    Details of Data Sources/connections cannot be viewed... 

    And the video is to small for my eyes. I tried to change resolution both in monitor and browser but without luck

     I see you are using Web Browser Form.
    Could you try to change to to Always from default:

    • (default) Only when necessary for correct rendering of the form (recommended)

    in Postback settings of fields' controls properties (Advanced tab)? In Infopath Designer, focus/click on control upon which press Alt+Enter...



    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 08-17-2012 05:03 AM In reply to

    Re: Refresh InfoPath field after code-based calculation

    bluewater:

    // output now has the correct value

    XPathNavigator outputNav = main.SelectSingleNode("my:meineFelder/my:output", NamespaceManager);

     

    Feeling me stupid but I cannot find output data field under  meineFelder data source in your template...


     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 08-17-2012 08:17 AM In reply to

    Re: Refresh InfoPath field after code-based calculation

    I am attaching the Form Code (it is a .cs file but I had to rename it to .txt so I could upload). I now realize that I have changed the form since I posted the question - the current version (correctly) references the "penalty" field.

    I have also updated the video to a more eye-friendly size (http://www.screencast.com/t/OipKSsfdx)

    Have changed the post-back options to "always" as per your recommendations, but no joy.

    Generally speaking, can you confirm that what I am trying to do (calculate a field in code and have the *browser* display refresh automatically) can actually be done at all or am I just chasing my tail here?

    Thx,

    bluewater

  • 08-17-2012 08:51 AM In reply to

    Re: Refresh InfoPath field after code-based calculation: Solved

    Upon further testing I found out that it works when you set both fields (the triggering *and* the triggered field) to "always", as you suggested. Thank you very much, this has helped me a lot!
  • 08-17-2012 09:28 AM In reply to

    Re: Refresh InfoPath field after code-based calculation

     I cannot find the .cs (renamed to .txt) though I believe I reproduced the problem

    When one textbox looses the focus another textbox is filled from code-behind in Preview but not in browser.

    And I believe I can find a solution - it is necessary to add a rule ...


    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 08-17-2012 10:45 AM In reply to

    Re: Refresh InfoPath field after code-based calculation

     For me it started to work in Mozilla Firefox and IE7 after I enabled Always for Postback settings on source textbox (which value changing triggers filling from code behind another target textbox)    

    I also succeeded with more complicated solutions...  

     

    Update:  

    Ooops, I saw that you have resolved the problem only after this post. Though for me it works with setting Postabk to alwaus only at one textbox

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
Page 1 of 1 (10 items)
Copyright © 2003-2012 Qdabra Software. All rights reserved.
View our Terms of Use.