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();
}