Calling a WebService in form code when button is clicked - InfoPath Dev
in

InfoPath Dev

Calling a WebService in form code when button is clicked

Last post 10-09-2013 10:41 AM by NicolasA. 13 replies.
Page 1 of 1 (14 items)
Sort Posts: Previous Next
  • 02-20-2009 01:45 PM

    Calling a WebService in form code when button is clicked

    Hi,

     

    I have designed an infopath form which has repeating table and a button "Insert Data" button. Insert Data button has a rule "Submit using  a data connection" which used a Webservice to insert data into SQL Server. But this works for first row in repeating table. If there are multiple rows in repeating table, data get inserted for first row only. Is there a way to add some rule so that data from all rows get inserted into DB. If yes, please help me

    If not,there must be a way to do call Webservice when button is clicked. I am using Infopath 2007, SQL Server 2005 and Visual Studio 2005. Can somebody help me how to traverse/read each field in a row through the repeating table and then call webservice to post data to SQL Server.

    Thanks a lot in advance :)

     

     

     

  • 02-23-2009 07:15 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    Hi,

    I am still trying to figure it out. Please help.

    Thanks a lot in advance,

     

  • 02-23-2009 07:49 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    What kind of webservice are you using?  Is it one you made or one you obtained from somewhere?  What are the input datatypes for the web method you are calling?  If it accepts a repeating (array/list) type, you can probably submit everything with just rules.  Otherwise, code may be the way to go.  If that's the case, please tell us what kind of code you are using/looking to use.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 02-23-2009 08:04 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    Hi Jimmy,

    Thanks a lot for replying. I have created my own webservice. It creates a connection to SQL Server database and then insert row into a table. I have created this webservice by looking at examples on InfopathDev and googling. And i could not find example where a webservice can accept array.

    I have written this webservice in C# .NET using Visual studio 2005. This webservice accept EmpName and DeptName from Infopath form. Using Empname it queries database to get EmpID. Then it inserts EmpID and DeptName into another table in database.

    So if you can point me to few examples of webservices which can accept an array that would be helpful. Or if you can help me how to loop through a repating table and call a webservice on a button click that would be great.

    Thanks a lot again :)

  • 02-23-2009 08:55 PM In reply to

    Re: Calling a WebService in form code when button is clicked

    It's probably simpler to just go with the original idea you had and skip the arrays, though I do suggest you do some searching around to learn more about web services.

    I have a web service that takes two strings as parameters.  These are hooked up in the submit data connection to the my:value1 and my:value2 nodes in my form.  My form's data source looks like this:

    • myFields
      • my:values (repeating group)
        • my:val1
        • my:val2
      • my:value1
      • my:value2

    The code below goes through each node in the values group, copies its respective values to my:value1 and my:value2, and invokes the submit.  The following code is C# code for InfoPath 2007:

    XPathNavigator mainDataNavigator = MainDataSource.CreateNavigator();

    // Get objects for the two value nodes hooked up to the data connection
    XPathNavigator submitValue1 = mainDataNavigator.SelectSingleNode("my:myFields/my:Value1", NamespaceManager);
    XPathNavigator submitValue2 = mainDataNavigator.SelectSingleNode("my:myFields/my:Value2", NamespaceManager);

    // Loop through each node of the repeating group
    foreach (XPathNavigator values in mainDataNavigator.Select("my:myFields/my:values", NamespaceManager))
    {
         // copy the values from this group's nodes to the submit fields
         submitValue1.SetValue(values.SelectSingleNode("my:val1", NamespaceManager).Value);
         submitValue2.SetValue(values.SelectSingleNode(
    "my:val2", NamespaceManager).Value);

         // Invoke the data connection
         DataConnections["Web Service Submit"].Execute();
    }
    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 02-24-2009 05:25 PM In reply to

    Re: Calling a WebService in form code when button is clicked

    Hi Jimmy,

     Thank you so much. I am able to finish 95% of my stuff.

    The only remaining thing is that data from first row is getting inserted into database. So if i add three rows to the repeating table and hit submit button, then values from first row get inserted 3 times into database. I debug my code while looping through each row and check that i am able to read value from each row. But DataConnection["Web Service Submit"].Execute() inserts data from first row only. How this can be resolved?

    I could not find anything to dispose DataConnections.

    Thanks a lot again.

     

     

  • 02-24-2009 06:56 PM In reply to

    Re: Calling a WebService in form code when button is clicked

    Are you copying the values from the repeating section to non-repeating nodes before submitting, or is the Submit connection hooked up directly to the repeating table? If you would paste your code here I could probably show you what's going wrong.

     Just as a side note, while this type of solution may work for a small project, if you plan to do a lot of this type of work you may want to consider using a more generalized piece of software to take care of this. Qdabra (the company that runs this site) has a web service called DBXL that handles submitting data from InfoPath forms to a database and a whole bunch of other features as well.  I recommend checking out the free trial version.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 02-25-2009 06:36 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    Hi Jimmy,

    Thanks a lot for replying. I am just trying to create a Proof of Concept for Timecard app for my team and its pretty small. If this needed to be used across multiple teams, i will definitely discuss using Qdabra webservice.

    This is the code i wrote. My Data connection Name is "Data_Submit". The webservice associated to this connection expects 2 parameters. So first i count the total number of repeated section and then run a for loop.

     

    XPathNavigator root = MainDataSource.CreateNavigator();
                    XPathNodeIterator iter = root.Select("/dfs:myFields/dfs:dataFields/d:EE_Prod_Cat_Task", NamespaceManager);
                    int myCount = iter.Count;  // to count number of repeated sections at run time


                    for (i = 1; i <= myCount; i++)
                    {
                                        XPathNavigator node = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:EE_Prod_Cat_Task["+ i.ToString()+"]/d:Employee/@EmployeeName", NamespaceManager);

    string EmpName = node.Value;


                                           XPathNavigator productNode = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:EE_Prod_Cat_Task[" + i.ToString() + "]/d:Product/@ProductName", NamespaceManager);

    string Product = productNode.Value;
                     
                    DataConnections["Data_Submit"].Execute();
                }

     

    Thanks a lot again :)  

  • 02-25-2009 06:57 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    Ok, there are a few problems here.

    The main problem is that in each interation of the loop, you need to store the relevant values in fields in the form to which the data connection is hooked up (in the data connection's settings).  Right now, you're just temporarily storing the references to the nodes in variables that never get used.  You should be able to use my code with very few modifications.  Here it is a little more closely tailored to your data source:

    XPathNavigator mainDataNavigator = MainDataSource.CreateNavigator();

    // Get objects for the two value nodes hooked up to the data connection
    XPathNavigator employeeNode= mainDataNavigator.SelectSingleNode("create a field to hold the employee name value for the submit data connection and put its XPath here", NamespaceManager);
    XPathNavigator productNode = mainDataNavigator.SelectSingleNode("create a field to hold the product name and put its XPath here", NamespaceManager);

    // Loop through each node of the repeating group
    foreach (XPathNavigator values in mainDataNavigator.Select("/dfs:myFields/dfs:dataFields/d:EE_Prod_Cat_Task", NamespaceManager))
    {
         // copy the values from this group's nodes to the submit fields
         employeeNode.SetValue(values.SelectSingleNode("d:Employee/@EmployeeName", NamespaceManager).Value);
         productNode.SetValue(values.SelectSingleNode(
    "d:Product/@ProductName", NamespaceManager).Value);

         // Invoke the data connection
         DataConnections["Data_Submit"].Execute();
    }
    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 02-25-2009 07:31 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    Hi Jimmy,

    DataConnection is hooked up to the fields EmployeeName, ProductName fields in dataFields group. And when this form is designed on database. EmployeeName and ProductName fields are populated from SQL Server database. I tried adding fields to store values as you suggested, but when i right click in datasource pane, i can not add fields to it.

    I also tried

    XPathNavigator root = MainDataSource.CreateNavigator();
                    XPathNodeIterator iter = root.Select("/dfs:myFields/dfs:dataFields/d:EE_Prod_Cat_Task", NamespaceManager);
                  
                    while (iter.MoveNext())
                    {
                                     

                        XPathNavigator node = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:EE_Prod_Cat_Task/d:Employee/@EmployeeName", NamespaceManager);
                        string EmpName = node.Value;
                        XPathNavigator productNode = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:EE_Prod_Cat_Task/d:Product/@ProductName", NamespaceManager);
                        string Product = productNode.Value;
          DataConnections["Data_Submit"].Execute();
                       }

    My main problem is how to make it go to the next repeating section. Its just stuck with first section. There should be a way to iterate through all repeating section and read values from it.

    Thanks a lot again for your valuable time :) 

     

     

  • 02-25-2009 08:10 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    Hi Jimmy,

    Its done. I am able to add fields and store values as you suggested. I can not thank you enough for your help :)

    Thank you so much,

     

  • 02-25-2009 08:17 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    Great to hear that.  I wish you the best of luck with your form.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 06-16-2009 01:55 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    HI Guys,

     This is govind, new to InfopathDev. I read this forum i created an infopath form for submitting multiple rows at a time. Actually i am getting an error "InfoPath cannot submit the form. An error occurred while the form was being submitted". But the records are submitted to the database using the webservice.. I need your help to fix this error.. Can anyone send me some idea to this..

    Thanks 

     

  • 10-09-2013 10:41 AM In reply to

    Re: Calling a WebService in form code when button is clicked

    Hello Jimmy: I am trying to save the infopath form data to a sql database via WCF webservice and hoping to save a copy to the sharepoint document library as well . I have already created a wcf web service , I tried to implement your method but nothing seems to be happening. I am new to infopath and I am trying to achieve both objectives. All this has to be done in code behind and I have thus far only implemeted your method, and not the saving to a sharepoint doc lib method yet. Please see the code I have written: using Microsoft.Office.InfoPath; using System; using System.Xml; using System.Xml.XPath; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using System.Text; using System.Diagnostics; using System.Collections.Generic; using System.Web; using System.Windows.Forms; namespace ProjectConceptQAReport { public partial class FormCode { // Member variables are not supported in browser-enabled forms. // Instead, write and read these values from the FormState // dictionary using code such as the following: public void InternalStartup() { ((ButtonEvent)EventManager.ControlEvents["CTRL3_5"]).Clicked += new ClickedEventHandler(CTRL3_5_Clicked); EventManager.FormEvents.Submit += new SubmitEventHandler(FormEvents_Submit); ((ButtonEvent)EventManager.ControlEvents["CTRL4_5"]).Clicked += new ClickedEventHandler(CTRL4_5_Clicked); ((ButtonEvent)EventManager.ControlEvents["bttn_Submit"]).Clicked += new ClickedEventHandler(bttn_Submit_Clicked); } public void CTRL3_5_Clicked(object sender, ClickedEventArgs e) { // Write your code here. } public void FormEvents_Submit(object sender, SubmitEventArgs e) { // If the submit operation is successful, set // e.CancelableArgs.Cancel = false; // Write your code here. } public void CTRL4_5_Clicked(object sender, ClickedEventArgs e) { // Write your code here. } //public void bttn_Submit_Clicked(object sender, ClickedEventArgs e) //{ // SaveData(); //} //public void FormEvents_Submit(object sender, SubmitEventArgs e) //{ // SaveData(); //} //public void SaveData() //{ // try // { // //save file to the location and file name as in xml // XPathNavigator xnDoc = this.MainDataSource.CreateNavigator(); // string saveLocation = xnDoc.SelectSingleNode("/my:ProjectConceptQAReport/my:SaveLocation", NamespaceManager).Value; // //File Name // xnDoc.SelectSingleNode("/my:ProjectConceptQAReport/my:FormName", NamespaceManager).SetValue(xnDoc.SelectSingleNode("/my:ProjectConceptQAReport/my:txt_ProjId", NamespaceManager).Value + "Project Concept QA Report" + xnDoc.SelectSingleNode("/my:ProjectConceptQAReport/my:CurrentDate", NamespaceManager).Value); // string fileName = xnDoc.SelectSingleNode("/my:ProjectConceptQAReport/my:FormName", NamespaceManager).Value; // SaveFormToSPList(MainDataSource.CreateNavigator().OuterXml, fileName, saveLocation, true); // } // catch (Exception ex) // { // SetSystemError("Error in 'SaveData:' " + ex.Message + " --- " + ex.InnerException); // } //} public void bttn_Submit_Clicked(object sender, ClickedEventArgs e) { //add your code here XPathNavigator mainDataNavigator = MainDataSource.CreateNavigator(); // Generate a name for the form to be saved in SharePoint //string Infopath_Form_Library = Guid.NewGuid().ToString(); // Set the name of the form on the data connection //root.SelectSingleNode( // "//my:ProjectConceptQAReport", NamespaceManager).SetValue(ProjectConceptQAReport); // Get objects for the two value nodes hooked up to the data connection //XPathNavigator submitValue1 = mainDataNavigator.SelectSingleNode("my:myFields/my:Value1", NamespaceManager); //XPathNavigator submitValue2 = mainDataNavigator.SelectSingleNode("my:myFields/my:Value2", NamespaceManager); XPathNavigator submitValue1 = mainDataNavigator.SelectSingleNode("my:myFields/my:txt_ProjId", NamespaceManager); XPathNavigator submitValue2 = mainDataNavigator.SelectSingleNode("my:myFields/my:txt_ProjTitle", NamespaceManager); XPathNavigator submitValue3 = mainDataNavigator.SelectSingleNode("my:myFields/my:txt_ProjDate", NamespaceManager); XPathNavigator submitValue4 = mainDataNavigator.SelectSingleNode("my:myFields/my:ddl_Quality", NamespaceManager); XPathNavigator submitValue5 = mainDataNavigator.SelectSingleNode("my:myFields/my:ddl_Decision", NamespaceManager); XPathNavigator submitValue6 = mainDataNavigator.SelectSingleNode("my:myFields/my:ddl_Strategic", NamespaceManager); XPathNavigator submitValue7 = mainDataNavigator.SelectSingleNode("my:myFields/my:ddl_Relevant", NamespaceManager); XPathNavigator submitValue8 = mainDataNavigator.SelectSingleNode("my:myFields/my:ddl_Monitoring", NamespaceManager); XPathNavigator submitValue9 = mainDataNavigator.SelectSingleNode("my:myFields/my:ddl_Efficient", NamespaceManager); XPathNavigator submitValue10 = mainDataNavigator.SelectSingleNode("my:myFields/my:ddl_Effective", NamespaceManager); XPathNavigator submitValue11 = mainDataNavigator.SelectSingleNode("my:myFields/my:ddl_Sustainability", NamespaceManager); XPathNavigator submitValue12 = mainDataNavigator.SelectSingleNode("my:myFields/my:txt_qaSummary", NamespaceManager); XPathNavigator submitValue13 = mainDataNavigator.SelectSingleNode("my:myFields/my:Doc_Status", NamespaceManager); // Loop through each node of the repeating group foreach (XPathNavigator values in mainDataNavigator.Select("my:myFields/my:values", NamespaceManager)) { // copy the values from this group's nodes to the submit fields submitValue1.SetValue(values.SelectSingleNode("my:txt_Notes", NamespaceManager).Value); submitValue2.SetValue(values.SelectSingleNode("my:txt_Date", NamespaceManager).Value); submitValue3.SetValue(values.SelectSingleNode("my:ProjManager/pc:Person/pc:DisplayName", NamespaceManager).Value); submitValue4.SetValue(values.SelectSingleNode("my:ProjManager/pc:Person/pc:AccountId", NamespaceManager).Value); submitValue5.SetValue(values.SelectSingleNode("my:ProjManager/pc:Person/pc:AccountType", NamespaceManager).Value); // Invoke the data connection DataConnections["WebServiceSubmitQA"].Execute(); } } } }
Page 1 of 1 (14 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.