Help with OnVersionUpgrade event - InfoPath 2003 / C# - InfoPath Dev
in

InfoPath Dev

Help with OnVersionUpgrade event - InfoPath 2003 / C#

Last post 11-17-2009 12:30 PM by mstaples709. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 11-16-2009 05:04 PM

    Help with OnVersionUpgrade event - InfoPath 2003 / C#

    Hello all,

    I have a client using an InfoPath 2003 managed code solution (C#).  Recently, we needed to make a change to the XML schema serving as the Main data source.  Specifically, we changed a single tag's name from <Title/> to <ClientTitle/>.  This change needs to be pushed out to their Production environment (WSS 3), but I am concerned about existing forms failing to open, due to the inevitable XML schema validation error.  If it were only a few forms, I would just change them manually, then re-upload.  However, they have several hundred items in their list.  I would like to take advantage of the Form object's OnVersionUpgrade event to replace the old tag with the new one.  I have seen this done quite easily in both InfoPath 2003 and 2007, using JScript or VB.NET.  I cannot seem to figure out how to do it in C#, though, since I am not very familiar with this language.  The JScript example given by Microsoft (and re-posted on this site a couple of times) does not seem to work in C#.  I have tried using the replaceChild() and appendChild() methods of IXMLDOMNode, but no luck so far.

    Any suggestions would be greatly appreciated.  Is it just me, or is there not much managed code support for C#?  Seems like everyone is using JS or VB.NET...

  • 11-16-2009 05:14 PM In reply to

    Re: Help with OnVersionUpgrade event - InfoPath 2003 / C#

     Would you mind letting us in on the working code samples you've found?  Maybe someone here would be able to adapt it to C#.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-16-2009 05:53 PM In reply to

    Re: Help with OnVersionUpgrade event - InfoPath 2003 / C#

     Sure thing...samples in JScript and VB.NET included below.

     

    JScript (taken from http://msdn.microsoft.com/en-us/library/aa662354%28office.11%29.aspx):

    function XDocument::OnVersionUpgrade(eventObj)
    {
    if (!XDocument.DOM.selectSingleNode("/Customers/CustomerInfo/EmailAddress"))
    {
    try
    {
    // Create the new element.
    var objItemNode = XDocument.DOM.selectSingleNode("/Customers/CustomerInfo")
    .ownerDocument.createElement("EmailAddress");

    // Add the new <item> element to the XML document as a
    // child of the <order> element.
    XDocument.DOM.selectSingleNode("/Customers/CustomerInfo")
    .appendChild(objItemNode);
    eventObj.ReturnStatus=true;
    }
    catch(ex)
    {
    XDocument.UI.Alert("There was an error inserting the " +
    "<EmailAddress> node.\nDescription: " + ex.description);
    eventObj.ReturnStatus=false;
    }
    }
    }
     
    VB.NET (InfoPath 2007):
    Public Sub FormEvents_VersionUpgrade(ByVal sender As Object, ByVal e As VersionUpgradeEventArgs)
    Dim xMain As XPathNavigator = MainDataSource.CreateNavigator()
    Dim clientTitlePath As XPathNavigator
    clientTitlePath = xMain.SelectSingleNode("/FormData/ClientInfo/ClientTitle", NamespaceManager)
    If clientTitlePath Is DBNull.Value Or clientTitlePath Is Nothing Then
    xMain.SelectSingleNode("/FormData/ClientInfo", NamespaceManager).AppendChild("<ClientTitle/>")
    End If
    End Sub
     
  • 11-16-2009 06:18 PM In reply to

    Re: Help with OnVersionUpgrade event - InfoPath 2003 / C#

     The equivalent C# code for that VB.NET code should be the following:

    XPathNavigator xMain = MainDataSource.CreateNavigator();
    XPathNavigator clientTitlePath;
    clientTitlePath = xMain.SelectSingleNode("/FormData/ClientInfo/ClientTitle", NamespaceManager);
    if(clientTitlePath == null)
    {
    xMain.SelectSingleNode("/FormData/ClientInfo", NamespaceManager).AppendChild("<ClientTitle/>");
    }
    That should succeed in creating a new ClientTitle node (assuming that a node actually exists at the path /FormData/ClientInfo), but it will not move the data from underneath Title to the new ClientTitle node.
    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-16-2009 06:38 PM In reply to

    Re: Help with OnVersionUpgrade event - InfoPath 2003 / C#

    Thanks, Jimmy.  This should work fine in InfoPath 2007 and I will let you know, once I have tested it.  Would you happen know what the equivalent C# code would be, using the InfoPath 2003 object model (i.e. thisXDocument)?  I am having some difficulty figuring this out.

  • 11-17-2009 04:21 AM In reply to

    Re: Help with OnVersionUpgrade event - InfoPath 2003 / C#

    This should be the equivalent of the JScript code you posted.  Only a few differences here and there:

                if (thisXDocument.DOM.selectSingleNode("/Customers/CustomerInfo/EmailAddress") == null)
                {
                    try
                    {
                        // Create the new element.
                        IXMLDOMNode objItemNode = thisXDocument.DOM.createElement("EmailAddress");

                        // Add the new <item> element to the XML document as a
                        // child of the <order> element.
                        thisXDocument.DOM.selectSingleNode("/Customers/CustomerInfo")
                           .appendChild(objItemNode);
                        e.ReturnStatus = true;
                    }
                    catch (Exception ex)
                    {
                        thisXDocument.UI.Alert("There was an error inserting the " +
                           "<EmailAddress> node.\nDescription: " + ex.Message);
                        e.ReturnStatus = false;
                    }
                }

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-17-2009 12:30 PM In reply to

    Re: Help with OnVersionUpgrade event - InfoPath 2003 / C#

    Thanks, Jimmy.  This code works great for appending a child node to the selected parent node.  The problem I have now is trying to replace or remove a child node.  Basically, what I am doing is renaming an element from "/ClientInfo/Title" to "/ClientInfo/ClientTitle".  I have tried the replaceChild method, along with a combination of appendChild and removeChild, with no luck.  The schema has changed, such that the new version contains the renamed "ClientTitle" element, while the previous version contains the old "Title" element.  Any existing forms based on the old schema fail to open due to the validation error with the new schema.  I thought that this was the whole point of the OnVersionUpgrade event...i.e. prior to schema validation, update the old form schema to use the new one and make the necessary changes to the form XML so that it is schema valid.  I have tested that the "Title" tag does exist in the old form, but when I try to use replaceChild, I get an error ("insert position node must be a child of the node to insert under").  I have verified the XPath via an alert and it is correct.  Here is a sample of the code:

    if (thisXDocument.DOM.selectSingleNode("/ClientInfo/Title") != null)

    {

    thisXDocument.UI.Alert("Found It");
    IXMLDOMNode oldTitleNode = thisXDocument.DOM.createElement("Title");
    IXMLDOMNode clientTitleNode = thisXDocument.DOM.createElement("ClientTitle");
    thisXDocument.DOM.selectSingleNode("/ClientInfo").replaceChild(clientTitleNode, oldTitleNode);

    }

    Any suggestions would be more than welcome.  Thanks!

Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.