Tuesday, March 6, 2012

Submit InfoPath Form Data to Web Service or WCF in InfoPath 2010

when we create a InfoPath form and configure the submit option to allow the user submit the form
to Web service or WCF(windows communication Foundation).






when you construct the data connection by following the data connection wizard, please ensure
you select Entire Form(XML document, Including processing instruction), when you decide to
submit the form to web service and parse the data in the web service.



in the web service you will need to have the following code snippet to parse the xml data and process it and push them back to the Database

 [WebMethod]
        public void SubmitDocument(XmlDocument doc)
        {
            XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
            nsManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-28T14:26:46");
            nsManager.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");

            XmlNode root = doc.DocumentElement;
            XmlNode node = root.SelectSingleNode("/dfs:IPDocument/my:myFields", nsManager);
            string recycleItemID = node.SelectSingleNode("/dfs:IPDocument/my:myFields/my:RecycleItemID", nsManager).InnerText;
            string recycleItemName = node.SelectSingleNode("/dfs:IPDocument/my:myFields/my:RecycleItemName", nsManager).InnerText;
            string recycleItemDesc = node.SelectSingleNode("/dfs:IPDocument/my:myFields/my:RecycleItemDescription", nsManager).InnerText;
            string preferredMeasurmentUnitID = node.SelectSingleNode("/dfs:IPDocument/my:myFields/my:PreferredMeasurementUnitID", nsManager).InnerText;

            RecycleItem recycleItemObj = new RecycleItem();
            recycleItemObj.RecycleItemID = recycleItemID;
            recycleItemObj.RecyleItemName = recycleItemName;
            recycleItemObj.RecycleItemDescription = recycleItemDesc;
            recycleItemObj.PreferredMeasureUnitID = Convert.ToInt32(preferredMeasurmentUnitID);

            Update(recycleItemObj);

        }

since we need to have the My namespace for parsing and processing in the code. usually i will use the debug mode to get the My Namespace by the trapping the submit XMLDocument in visual studio 2010. I just figure out we can retrieve the My Namespace value in the InfoPath Designer.

it is very simple and straightforward. select the group field name MyFields, then right click to view the Properties.




click on the Details tab. you will find the Namespace of MyFields. the Namespace is fixed for this group after it is created.

1 comment:

  1. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.

    Form Processing Services

    ReplyDelete