Programmatically update Infopath Form XML using c# code

This article would share a code-snippet that updates the Infopath form XML.  In my portal, I’m performing data-acquision using Infopath forms for a Infopath-content type workflow scenario. This scenario is all about an employee appraisal workflow. I’ve a business requirement to mark the ‘IsManager’ column as ‘Yes’ , when the employee gets promoted as Manager. This forced me to think a way to programmatically update the Infopath form XML using c# code. The following is the c# code that can be used to programmatically update the Infopath Form XML.

                        SPWeb   oWeb = SPContext.Current.Web;                       
                        SPList _list = oWeb.Lists[“TestFormLib”];
                        MemoryStream oMemoryStream = new MemoryStream(item.File.OpenBinary());
                        XmlTextReader oReader = new XmlTextReader(oMemoryStream);

                        XmlDocument oDoc = new XmlDocument();


                        XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(oDoc.NameTable);
                        nameSpaceManager.AddNamespace(“my”, ““);

                        doc.DocumentElement.SelectSingleNode(“my:IsManager”, nameSpaceManager).InnerText = “Yes”;
                        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                        SPFile oSPFile = oWeb.Folders[“TestFormLib”].Files.Add(item.File.Name.ToString(), (encoding.GetBytes(doc.OuterXml)), true);


 Subscribe to my blog

4 thoughts on “Programmatically update Infopath Form XML using c# code

  1. Pingback: Programmatically update Infopath Form XML using c# code - My experiments with SharePoint, Azure and .NET using Visual Studio

  2. You inspired me so i bring you my solution

    //NOTE : You may also need to use web.allowUnsafeUpdate = true;

    SPList myList = SPContext.Current.Web.Lists[“myList”];
    SPListItem item = myList.GetItemById(123);

    XmlDocument xmlItem = new XmlDocument();

    //Open XML file and load it into XML document
    using (Stream streamItem = item.File.OpenBinaryStream())

    XPathNavigator navItem = xmlItem.CreateNavigator();

    //Redefine NameSpaceManager (Generate the NameSpace manager for this item)
    XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());

    foreach (KeyValuePair ns in navItem.GetNamespacesInScope(XmlNamespaceScope.All))
    if (ns.Key == String.Empty)
    nsManager.AddNamespace(“def”, ns.Value);
    nsManager.AddNamespace(ns.Key, ns.Value);

    //Change your value here….
    navItem.SelectSingleNode(“/my:myFields/my:yourFieldName”, nsManager);

    //Save the modified xml into the item
    byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(navItem.OuterXml);

    Have a nice day!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.