How can I read XML from a dataset field?

R

redbastid

Hi!

I am retrieving a dataset from a webservice as my main data source for
an infopath form. I added a button to the repeating table that
displays the data, and when clicked, I want it to either:

a) open a new infopath form using an XML string that is in the dataset
as a field, or

b) display the XML string that is in the dataset as a field on a form
in an alternate view in the same infopath form.

I am having problems retrieving the specific values from the row on
which the button was placed. I can use a pair of rules to set a couple
of nodes I appended to the schema and set some text boxes to their
values, but displaying the XML in a text box is not what I want. I
can't seem to set anything other than an element with a rule.

So, my question is: Given a dataset with ColA, ColB and ColC, how can I
get the specific value of ColB on the row where the button was clicked?
And, given that these forms will be served off a web server, should I
use the alternate view to display the row's XML data field content, or
spawn another form?

Many thanks for any help you can provide in this urgent matter!
 
S

Scott L. Heim [MSFT]

Hi,

By having a button on each row of your table, you can now use the
"eventObj" argument in code to get what you need - here is an example using
VBScript:

- I have a web service that pulls data from the Northwind Sample database -
specifically the Customers table
- When I look at my Data Source Task Pane, here is what I see under
myFields->dataFields:

- tns:GetCustOrdersResponse
- GetCustOrdersResult
- ns1:ds
- (choice)
- Customers

- I then added a Repeating Table to my form and bound it to the Customers
table
- I added a new column to this table and placed a button in the new column
- Now, in the click event for my button I see the following default
structure:

Sub CTRL46_6_OnClick(eventObj)

End Sub

In order to display the CompanyName field from the Repeating Table, I added
the following code:

Dim objCompanyName
Set objCompanyName = eventObj.Source.selectSingleNode("ns1:CompanyName")
XDocument.UI.Alert objCompanyName.text

By using "eventObj.Source.selectSingleNode" we already have a reference to
the current row we are on - as such, we simply need to get to the field we
want.

Here's a trick to confirm the "namespace" information you will need to use:

- Display the Controls Task Pane
- From the Advanced section, choose Expression Box
- Click the "fx" button
- Click the Insert Field or Group button
- Drill down through the data structure, highlight the field you want to
return and click OK
- Click the "Edit XPath" option - you should see the namespace needed for
that node.

I hope this helps!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

redbastid

That "Edit XPath" option you described was a lifesaver! I can now
select the data from the row! Thank you very much!

Now, how can I spawn a second Infopath form? I want to prepopulate the
query data objects and run the query automatically, so when the form
comes up, its all set for the user to make changes and submit them.
(I'm using the Visual Studio tools for this, btw) When I try to create
a new process though, it blows up before I even reach the line "Process
proc = new Process();". With that line commented out (and the
subsequent ones), it works fine. Any ideas or help on this would be
much appreciated! I pasted the two snippets of code I am using to
attempt this.

And thank you again for the previous tip, as it has helped me
immensely!

In the form code, I have:

Process Proc = new Process();
Proc.StartInfo.FileName = "cscript script.js " + sRecordID;
Proc.StartInfo.Verb = "Run";
Proc.StartInfo.CreateNoWindow = true;
Proc.Start();

The content of the jscript file is:

if(WScript.Arguments.count()==1)
{
var param1 = WScript.Arguments.Item(0);
}

//Start the application
var oApp = new ActiveXObject("InfoPath.Application");

//Open an InfoPath document from the published template
var oXDocumentCollection = oApp.XDocuments;
var oXDocument =
oXDocumentCollection.NewFromSolution("C:\myOtherForm.xsn");

// Get pointers to the target fields
var oID =
oXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:queryFields/s0:WbSvc/s0:iRecordID");

//Update the fields
oID.text = param1;
 
S

Scott L. Heim [MSFT]

Hi,

I am glad that information helped! :)

With regard to spawning a new form based off an existing template, here is
some sample VBScript code. (I realize you are using VS.NET but I had this
handy - if you need assistance converting it let me know.)

** NOTE: I have an XSN named: NWCust.XSN which is bound to a web service
that returns Customers, Orders and Order Details information from the
Northwind sample database. The sample code below I am calling from a button
click event in another form altogether.

Dim strNWCust
Dim objNewIP
Dim objNewDoc

'Create a new instance of InfoPath
Set objNewIP = CreateObject("InfoPath.ExternalApplication")

'Variable to store the location of the form I want to open
strNWCust = "C:\IPTests\NWCust.xsn"

'Create the new form based on the above template
objNewIP.NewFromSolution strNWCust

'Get a reference to that XDocument
Set objNewDoc = Application.XDocuments(1)

'Set the SelectionNamespaces property so we can walk the DOM and set field
values
objNewDoc.DOM.setProperty "SelectionNamespaces", _

"xmlns:q=""http://schemas.microsoft.com/office/infopath/2003/ado/queryFields
""" _
& "
xmlns:d=""http://schemas.microsoft.com/office/infopath/2003/ado/dataFields""
" _
& "
xmlns:dfs=""http://schemas.microsoft.com/office/infopath/2003/dataFormSoluti
on""" _
& "
xmlns:my=""http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-08-1
5T18:01:30""" _
& " xmlns:tns=""http://tempuri.org/NWCustomersOrdersDetails/Service1"""

'Get a reference to the one query field I have for the CustomerID and
'set the value of that field to BERGS

objNewDoc.DOM.selectSingleNode("//dfs:myFields/dfs:queryFields/tns:GetCustOr
ders/tns:strCustID").text = "BERGS"

'Execute the query to fill the new document with data based on that
customer
objNewDoc.Query

'Clean up
Set objNewIP = Nothing
Set objNewDoc = Nothing

Let me know if this helps you!

Best regards,

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

redbastid

Hi! I'm having trouble instantiating the ExternalApplication object.

I tried this line:
Microsoft.Office.Interop.InfoPath.SemiTrust.ExternalApplication
objNewIP = new
Microsoft.Office.Interop.InfoPath.SemiTrust.ExternalApplication();

But I get a compilation error claiming it is inaccessible "due to its
protection level". Is there a different way I can create the object or
is there a way I can assign the proper protection level it expects?

Thanks again for you help!
 
S

Scott L. Heim [MSFT]

Hi,

In order for this to work, your form will need to be "fully trusted." This
can be accomplished using the "RegForm" tool from the InfoPath SDK or by
signing the template with a digital certificate. For testing purposes only,
you can create a "self-cert" that is only valid on your machine:

- Open your form in Design View
- From the Tools menu choose Form Options
- Select the Security tab
- Uncheck the option "Automatically determine security level..." and enable
the "Full Trust" option
- Enable the "Sign this form" option and if you do not have any valid
code-signing digital certificates on your machine, click the Create
Certificate button to create a "self cert." Once the creation is complete,
it should add the information to the Name, Issued By and Expiration Date
fields
- Click OK
- Save your form and test again.

Here is some additional information on fully trusted forms that you may
find of interest:

Understanding Fully Trusted Forms
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipsdk/html/
ipsdkUnderstandingFullyTrustedForms.asp

Using the Form Registration Tool
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipsdk/html/
ipsdkUsingTheFormRegistrationTool.asp

Deploy a Fully Trusted Form to a SharePoint Form Library
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipsdk/html/
ipsdkDeployURNFormToSharePoint_HV01086376.asp

InfoPath SDK
http://www.microsoft.com/downloads/details.aspx?FamilyId=351F0616-93AA-4FE8-
9238-D702F1BFBAB4&displaylang=en

InfoPath Technical FAQ
http://www.microsoft.com/technet/prodtechnol/office/office2003/plan/inpthfaq
..mspx#ECBAA

Let me know if this helps!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

redbastid

I tried the self-cert as you suggested, but I get an error saying that
"Extracted forms cannot be signed." and that I need to "Sign the form
and publish it". This error appears when I attempt to save the changes
to the form after I set the certificate.

I'm assuming that "extracted" refers to the fact that I imported the
form into Visual Studio from a version created in InfoPath alone. I
had to do this because I could not find a way to create the form from
Visual Studio using a web service datasource; it only wanted to use an
XML document/schema and every time I added the datasource it added it
as secondary. So, I created an empty form in InfoPath directly,
specifying the web service source and then imported into a new VS
project.

One of the suggestions I found was to not use the objects that required
trust, which in this case is
Microsoft.Office.Interop.InfoPath.SemiTrust. Is there a way I can open
the other form without using this and by using something else instead?

Thanks again!
 
R

redbastid

Also, I can't publish it because it won't compile. And I can't compile
it because I am unable to sign it or otherwise set the trust level.
It's a vicious cycle I can't seem to escape from right now. Any help
would be most appreciated.

Thanks again!
 
S

Scott L. Heim [MSFT]

Hi,

No - if you want to launch the other form, your solution must be fully
trusted. However, the reason you are seeing the error is because you are
attempting to sign the Manifest.XSF file. When you are developing with
.NET, the "bin" folder contains the "compiled" XSN - this is what you will
need to open in Design View and sign.

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top