Add default values using script

S

Scott L. Heim [MSFT]

Hi Marc,

Can you provide some additional details as to what you are trying to
accomplish? The reason I ask is that default values will only come into
play when a new record is added. As you know, you can set these through the
designer. What are you trying to implement where you would need to set
these programmatically?

In short, you do not have programmatic access to the controls but we may be
able to provide another route for you with a better understanding of what
you need to do.

Best regards,

Scott L. Heim
Microsoft Developer Support

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

Marc Nemegeer

Hi Scott,
In a nutshell, I'm trying to build a multi select list box using the
solution as described in the Infopath blog.

As the values are dependent on the value of a drop down the user selects at
run-time, I need to do it at run-time and I can't specify them at design time.

I see two solutions:
1) Set the default values using script to the values determined from my
secondary connection and filtered with the value entered in the drop down. I
fetch only one column, the description.

2) Take the same approach for fetching them, and afterwards add all values
as nodes to my main data source. One caveat, I need to add a boolean to
indicate selected/not selected, that is not present in my secondary data
source.

I was not successful in realising the script for the second approach,
possibly because I'm relatively new to developing script ... And I'm
realising the solution using C#, no script.

The first solution looks the most clean one, from a software development
perspective but if it is not supported by Infopath ...

Help would be appreciated.

I have an alternative solution of a repeating table with the drop down and
let the user add a row if he wants to add more than one selected item.

I am interested in getting one of the two approaches working, but at present
I don't have enough experience using the tools to accomplish one of the
solutions :)

Regards, and thanks for your response,
Marc
 
S

Scott L. Heim [MSFT]

Hi Marc,

I would think the repeating table option would be the easiest to implement
- is this not working for you either?

Scott L. Heim
Microsoft Developer Support

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

Marc Nemegeer

Hi Scott,

The repeating table is the second option I'm talking about ?

And no, this does not work. I'm able to get a table populated with all
values from a secondary connection. But then, I have two problems:
1) How do I copy this list of possible values from my secondary data source
to my primary data ?
2) And how do a add a check box to this repeating table for each row to
check or uncheck the value.

Regards,
Marc
 
S

Scott L. Heim [MSFT]

Marc,

What is your secondary data source? Database, web service, XML file, etc.

Thanks,

Scott L. Heim
Microsoft Developer Support

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

Marc Nemegeer

Scott,
My secondary data source is a table in a database with a filter and a sort
by description on it.
Regards,
Marc
 
S

Scott L. Heim [MSFT]

Hi Marc,

Would you be able to modify the database table to include another field? If
so, this would be the field you would use to bind to a check box field for
your Repeating Table. Then to move the "selected" records to your primary
datasource you would need to just get the nodes that are selected and could
most likely use the "ExecuteAction" method to copy those records to a
Repeating Table or Section on your main form.

Let me know if it's doable to add the additional field to your table and I
can provide you with sample steps.

Scott L. Heim
Microsoft Developer Support

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

Marc Nemegeer

Scott,
Yes, I can add a boolean field to my database table. If I really have to ...

An when I re-open the form the selected check boxes from the saved form will
be checked again ?

But nevertheless, I'm curious about your solution :)

Regards,
Marc
 
S

Scott L. Heim [MSFT]

Hi Marc,

OK - try these steps as documented to see if they provide you with what you
need!

- Create a new Access database named SelectedItemsTest.MDB
- Add a table (named Table1) with the following fields:

- ID (AutoNumber, Primary Key)
- MyDesc (Text)
- Selected (Yes/No)

- Add the following data entries to the table: Desc1, Desc2, Desc3, Desc4,
Desc5
- Create a new, blank InfoPath solution
- Add a secondary data connection to Table1
- From the Data Source Task Pane, select the Table1 Secondary Data Source,
expand dataFields, drag d:Table1 to your form and select Repeating Table.
- Select the Controls Task Pane
- Add a button below the table
- Add a Repeating Table to your form with just 1 field - your "Main" data
source should appear as follows:

- myFields
- group1
- group2
- field1

- Right-click on the button, choose Properties and click the Edit Form Code
button
- You should see the following:

function CTRL8_5::OnClick(eventObj)
{

}

- Add this code between the braces:

//Get a reference to the Table1 data connection
var objTable1DOM = XDocument.GetDOM("Table1");

//Set the SelectionNamespaces property of the Table1 data connection to
walk the DOM
objTable1DOM.setProperty("SelectionNamespaces",

'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSoluti
on" ' +

'xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"'
);

//Get the selected items
var objSelectedItems =
objTable1DOM.selectNodes("//dfs:myFields/dfs:dataFields/d:Table1[@Selected
= 'True']");

//Loop through the selected items
for(i=0;i<=objSelectedItems.length -1;i++)
{
//See if the first record in the destination Repeating Table is blank
if(XDocument.DOM.selectSingleNode("//my:group1/my:group2/my:field1").text
== "")
{
XDocument.DOM.selectSingleNode("//my:group1/my:group2/my:field1").text =
objSelectedItems(i).attributes(1).text;
}
else
{
//Add a new row
XDocument.View.ExecuteAction("xCollection::insert", "group2_1");
//Get a reference to that new row
var objNewItem =
XDocument.DOM.selectSingleNode("//my:group1/my:group2[last()]/my:field1");
//Set the text of the field
objNewItem.text = objSelectedItems(i).attributes(1).text;
}
}

- ** NOTE: In the ExecuteAction line, you will see a reference to: group2_1
- you will want to insure this is the same in your sample. Right-click on
the destination Repeating Table, choose Properties and then select the
Advanced tab - this will confirm your collection value.

- Save the code and test - you should be able to select 1 or more items
from the secondary data source table and add those to your main form table!
Now you may need to expand on this as I am not taking into consideration if
you immediately go back and add another selection that we just add that new
item. But this should at least get you going.

Let me know how this works for you!

Scott L. Heim
Microsoft Developer Support

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

Marc Nemegeer

Scott,

Yes, it works, you got me going :) This is the kind of information that is
not available, or at least I have not found it documented in a book or faq ...

Some minor bugs, for the record:
1) the for loop should go to objSelectedItems.length without subtracting 1
2) the objSelectedItems(i).attributes(1).text had to be modified to a 0

Thanks for your effort,
Marc


Scott L. Heim said:
Hi Marc,

OK - try these steps as documented to see if they provide you with what you
need!

- Create a new Access database named SelectedItemsTest.MDB
- Add a table (named Table1) with the following fields:

- ID (AutoNumber, Primary Key)
- MyDesc (Text)
- Selected (Yes/No)

- Add the following data entries to the table: Desc1, Desc2, Desc3, Desc4,
Desc5
- Create a new, blank InfoPath solution
- Add a secondary data connection to Table1
- From the Data Source Task Pane, select the Table1 Secondary Data Source,
expand dataFields, drag d:Table1 to your form and select Repeating Table.
- Select the Controls Task Pane
- Add a button below the table
- Add a Repeating Table to your form with just 1 field - your "Main" data
source should appear as follows:

- myFields
- group1
- group2
- field1

- Right-click on the button, choose Properties and click the Edit Form Code
button
- You should see the following:

function CTRL8_5::OnClick(eventObj)
{

}

- Add this code between the braces:

//Get a reference to the Table1 data connection
var objTable1DOM = XDocument.GetDOM("Table1");

//Set the SelectionNamespaces property of the Table1 data connection to
walk the DOM
objTable1DOM.setProperty("SelectionNamespaces",

'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSoluti
on" ' +

'xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"'
);

//Get the selected items
var objSelectedItems =
objTable1DOM.selectNodes("//dfs:myFields/dfs:dataFields/d:Table1[@Selected
= 'True']");

//Loop through the selected items
for(i=0;i<=objSelectedItems.length -1;i++)
{
//See if the first record in the destination Repeating Table is blank
if(XDocument.DOM.selectSingleNode("//my:group1/my:group2/my:field1").text
== "")
{
XDocument.DOM.selectSingleNode("//my:group1/my:group2/my:field1").text =
objSelectedItems(i).attributes(1).text;
}
else
{
//Add a new row
XDocument.View.ExecuteAction("xCollection::insert", "group2_1");
//Get a reference to that new row
var objNewItem =
XDocument.DOM.selectSingleNode("//my:group1/my:group2[last()]/my:field1");
//Set the text of the field
objNewItem.text = objSelectedItems(i).attributes(1).text;
}
}

- ** NOTE: In the ExecuteAction line, you will see a reference to: group2_1
- you will want to insure this is the same in your sample. Right-click on
the destination Repeating Table, choose Properties and then select the
Advanced tab - this will confirm your collection value.

- Save the code and test - you should be able to select 1 or more items
from the secondary data source table and add those to your main form table!
Now you may need to expand on this as I am not taking into consideration if
you immediately go back and add another selection that we just add that new
item. But this should at least get you going.

Let me know how this works for you!

Scott L. Heim
Microsoft Developer Support

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

Scott L. Heim [MSFT]

Hi Marc,

Thank you for the update - I am glad this worked for you!

Take care,

Scott L. Heim
Microsoft Developer Support

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

robot smiff

How would you change that code to source the list from a SharePoint list?

Thanks!
JK
 

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