Convert from 2007 Object model to 2003 Object Model (C#) - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Convert from 2007 Object model to 2003 Object Model (C#)

Last post 04-24-2009 09:46 AM by buck_murdock. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 04-23-2009 03:27 PM

    Convert from 2007 Object model to 2003 Object Model (C#)

    I've spent a lot of time building a form that I was hoping i could use as a Browser Enabled form.  The user would open the form which would run the C# code that queried the users computer using WMI to retrieve the information we wanted, and then the user would enter in their information and submit it to a Form Library.

    I just uploaded the form to my test server and realised that when i open this browser enabled for, it queries the server it's residing on rather than the users computer.  I thought I would just make this a regular form, because that seemed to work OK.  Unfortunately, I built this all with InfoPath 2007, so the Object Model in my code won't work on most of the computers we want to inventory.

    Is there a nice easy way to just convert the existing C# code into a 2003 OM friendly code?  Most of the code samples i used to piece together the form were from sites using the 2007 OM.  I thought i'd be able to just go through and change a few lines and it would be Ok, but it proved to be a little more involved than that, so i thought I would come here and ask for help.  I found this article http://blogs.msdn.com/infopath/archive/2006/06/06/619143.aspx and hoped i could just change things like XPathNavigator to IXMLDOMNode but the syntax is a little trickier than that so i need some help.

     Here's a sample of my code, if someone could just show me how to convert a small piece I can probably take it and run from there.  One is the more basic query, and the other is a query that would retrieve multiple items and enter them into a repeating table which is a little bit more involved.

    SAMPLE:

    public void InternalStartup()

    {

    EventManager.FormEvents.Loading +=
    new LoadingEventHandler(FormEvents_Loading);((ButtonEvent)EventManager.ControlEvents["InventoryButton"]).Clicked += new ClickedEventHandler(InventoryButton_Clicked);

    }

    public void FormEvents_Loading(object sender, LoadingEventArgs e)

    {

    ManagementObjectSearcher searcherAudio =

    new ManagementObjectSearcher("root\\CIMV2",

    "SELECT * FROM Win32_SoundDevice");

    ManagementObjectSearcher searcherMemory =

    new ManagementObjectSearcher("root\\CIMV2",

    "SELECT * FROM Win32_PhysicalMemory");

    }

    foreach (ManagementObject queryObj in searcherAudio.Get())

    {

    string audioDevice = queryObj["Name"].ToString();

    // Create an XPathNavigator to walk the main data source

    // of the form.

    XPathNavigator xnMyForm = this.CreateNavigator();

    XmlNamespaceManager ns = this.NamespaceManager;

    // Set the fields in the form.

    xnMyForm.SelectSingleNode("/my:myFields/my:SoundCard", ns)

    .SetValue(audioDevice);

    }

    foreach (ManagementObject queryObj in searcherMemory.Get())

    {

    string slot = queryObj["DeviceLocator"].ToString();

    string capacity = queryObj["Capacity"].ToString();

    string speed = queryObj["Speed"].ToString();

    // Create an XPathNavigator to walk the main data source

    // of the form.

    XPathNavigator xnMyForm = this.CreateNavigator();

    XmlNamespaceManager ns = this.NamespaceManager;

    // Set the fields in the form.

    xnMyForm.SelectSingleNode("/my:myFields/my:Memory/my:Ram/my:slot", ns)

    .SetValue(slot);

    xnMyForm.SelectSingleNode(
    "/my:myFields/my:Memory/my:Ram/my:capacity", ns)

    .SetValue(capacity);

    xnMyForm.SelectSingleNode(
    "/my:myFields/my:Memory/my:Ram/my:speed", ns)

    .SetValue(speed);

    // Add the item to the repeating table

    AddItemMemory(slot, capacity, speed);

    }

     

    // Remove the first empty item from the repeating table

    DeleteFirstEmptyItemMemory();

    private void AddItemMemory(string slot, string capacity, string speed)

    {

    XmlDocument doc = new XmlDocument();

    XmlNode group = doc.CreateElement("Ram",

    NamespaceManager.LookupNamespace("my"));

    XmlNode field = doc.CreateElement("slot",

    NamespaceManager.LookupNamespace("my"));XmlNode node = group.AppendChild(field);

    node.InnerText = slot;

    field = doc.CreateElement("capacity",

    NamespaceManager.LookupNamespace("my"));

    node = group.AppendChild(field);

    node.InnerText = capacity;

    field = doc.CreateElement(
    "speed",NamespaceManager.LookupNamespace("my"));

    node = group.AppendChild(field);

    node.InnerText = speed;

    doc.AppendChild(group);

    MainDataSource.CreateNavigator().SelectSingleNode(

    "/my:myFields/my:Memory",

    NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

     

    }

    private void DeleteFirstEmptyItemMemory()

    {

    XPathNavigator domNav = MainDataSource.CreateNavigator();

    XPathNavigator itemNav = domNav.SelectSingleNode(

    "/my:myFields/my:Memory/my:Ram[1]",

    NamespaceManager);

    if (itemNav != null)

    itemNav.DeleteSelf();

    }

  • 04-23-2009 05:42 PM In reply to

    Re: Convert from 2007 Object model to 2003 Object Model (C#)

    Yes, as you have noticed, the 2007 and 2003 OMs have significant differences, but once you get used to both, it's relatively easy to transition code from one to the other.  Here's a simple code example that does similar tasks to the ones in your example (set a single field value, add rows to a repeating group, delete a row from a repeating group):

    [InfoPathEventHandler(MatchPath = "CTRL7_5", EventType = InfoPathEventType.OnClick)]

    public void CTRL7_5_OnClick(DocActionEvent e)
    {
         DeleteFirstRow();
         thisXDocument.DOM.selectSingleNode(
    "/my:myFields/my:field4").text = "Yay!";
         IXMLDOMNode group3Node = thisXDocument.DOM.selectSingleNode("/my:myFields/my:group3");
         string myNamespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-04-23T23:25:04";

         foreach (string s in new string[] { "I", "am", "the", "walrus" })
         {
              IXMLDOMNode newGroup4Node = thisXDocument.DOM.createNode(DOMNodeType.NODE_ELEMENT, "group4", myNamespace);
              group3Node.appendChild(newGroup4Node);

              IXMLDOMNode newField = thisXDocument.DOM.createNode(DOMNodeType.NODE_ELEMENT, "field1", myNamespace);
              newField.text = s;
              newGroup4Node.appendChild(newField);

              newField = thisXDocument.DOM.createNode(
    DOMNodeType.NODE_ELEMENT, "field2", myNamespace);
              newField.text =
    "field 2";
              newGroup4Node.appendChild(newField);

              newField = thisXDocument.DOM.createNode(DOMNodeType.NODE_ELEMENT, "field3", myNamespace);
              newField.text = s;
              newGroup4Node.appendChild(newField);
         }
    }
    private void DeleteFirstRow()
    {
         IXMLDOMNode rowToDelete = thisXDocument.DOM.selectSingleNode("/my:myFields/my:group3/my:group4[1]");
         if (rowToDelete != null)
         {
              rowToDelete.parentNode.removeChild(rowToDelete);
         }
    }

    The lines in your code like this don't seem to make much sense:

    xnMyForm.SelectSingleNode("/my:myFields/my:Memory/my:Ram/my:slot", ns).SetValue(slot);
    xnMyForm.SelectSingleNode(
    "/my:myFields/my:Memory/my:Ram/my:capacity", ns).SetValue(capacity);
    xnMyForm.SelectSingleNode(
    "/my:myFields/my:Memory/my:Ram/my:speed", ns).SetValue(speed);

    They are in a loop, but they keep overwriting the same field until the last iteration, and then you delete the entire row.  Is this code you were planning to delete?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 04-24-2009 09:46 AM In reply to

    Re: Convert from 2007 Object model to 2003 Object Model (C#)

    Thanks for the samples, I'll poke around and see if i can fumble my way through and redo the lines of code so that it works with 2003.

     

    As for your other questions about the looping.  I have no idea.  Unfortunately I'm not a programmer.  I just find code samples on various forums and such.  i think i found these bits from: http://www.bizsupportonline.net/infopath2007/copy-rows-from-sharepoint-list-to-main-data-source.htm

     And when it did that i found it would always duplicate one of the rows and place it in the first row for some reason, and i found this article on how to delete that row: http://www.bizsupportonline.net/infopath2007/programmatically-delete-first-row-repeating-table-infopath.htm

    Like i said i dont know how to code, so i didnt know how to troubleshoot what it was doing and just found an article that seemed to do what i needed.  One of these days i'll get myself into a programming class and start learning how to do this a little better, but right now i'm just one of those people who knows just enough to make his life miserable ;-).

     

    Thanks again for the help, i'll give it a shot later today!!!

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