<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://www.infopathdev.com:443/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Code</title><link>https://www.infopathdev.com:443/forums/45.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 SP3 (Build: 31118.962)</generator><item><title>Re: A solution for inserting a row using code</title><link>https://www.infopathdev.com:443/forums/thread/29004.aspx</link><pubDate>Wed, 28 May 2008 18:31:03 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:29004</guid><dc:creator>Albert</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/29004.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=29004</wfw:commentRss><description>&lt;p&gt;The code reads the&amp;nbsp;node from sampledata.xml, which&amp;nbsp;contain all default values (= templated?).&amp;nbsp;But its possible to change the code, so instead of reading&amp;nbsp;a node from sampledata&amp;nbsp;it can read a node from the&amp;nbsp;maindom and clone it. It doesnt matter what if its the last or not. Its just a matter of selecting the right node with selectSingleNode and inserting it into the maindom. Maybe I could post an example later, something like &amp;#39;function CopyNode(xpath)&amp;#39; or &amp;#39;function CopyNode(nTemplate)&amp;#39;? &lt;/p&gt;
&lt;p&gt;Albert&lt;/p&gt;</description></item><item><title>Re: A solution for inserting a row using code</title><link>https://www.infopathdev.com:443/forums/thread/29002.aspx</link><pubDate>Wed, 28 May 2008 18:17:16 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:29002</guid><dc:creator>Clay Fox</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/29002.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=29002</wfw:commentRss><description>&lt;p&gt;Thanks Albert!&lt;/p&gt;
&lt;p&gt;A common desire is to insert a row templated from an existing row. So copy a row and then insert a clone to reduce the data entry. Would it be the last record or could you specify the sample row?&lt;/p&gt;
&lt;p&gt;Could your code be used for this?&amp;nbsp; Maybe you could explain as this would help others.&lt;/p&gt;</description></item><item><title>A solution for inserting a row using code</title><link>https://www.infopathdev.com:443/forums/thread/28988.aspx</link><pubDate>Wed, 28 May 2008 14:58:05 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:28988</guid><dc:creator>Albert</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/28988.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=28988</wfw:commentRss><description>&lt;p&gt;Hi, i&amp;#39;ve tried to look for some code that inserts rows. I couldn&amp;#39;t find any answer that satisfied me, for example missing node-order. So i&amp;#39;ve created my own method and just wanted to share it (jscript)&amp;nbsp;with everyone here.&lt;/p&gt;
&lt;p&gt;It&amp;nbsp;can:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div&gt;Insert a row to a repeatinggroup&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Insert a&amp;nbsp;group which is not in default values &lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Respect the node order InfoPath expects&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Use predicates, like my:repeating_group[3]/my:repeating_subgroup&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;div&gt;It can&amp;#39;t:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div&gt;Insert multiple rows, always the first node (selectSingleNode) will be used.&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Insert a row at a specific location in a repeating group, the new node is always inserted at the end of a repeating group.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;//Inserts a node at the specified location&lt;br /&gt;function InsertNode(xpath)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;//Get reference to the sampledocument (sampledata.xml)&lt;br /&gt;&amp;nbsp;var domSample = XDocument.CreateDOM();&lt;br /&gt;&amp;nbsp;domSample.async = false;&lt;br /&gt;&amp;nbsp;domSample.validateOnParse = false;&lt;br /&gt;&amp;nbsp;domSample.load(&amp;quot;sampledata.xml&amp;quot;);&lt;br /&gt;&amp;nbsp;domSample.setProperty(&amp;quot;SelectionNamespaces&amp;quot;, XDocument.DOM.getProperty(&amp;quot;SelectionNamespaces&amp;quot;));&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;//Get reference to sample node, this will be inserted into the maindom&lt;br /&gt;&amp;nbsp;var xpathSample = xpath;&lt;br /&gt;&amp;nbsp;while(xpathSample.indexOf(&amp;#39;[&amp;#39;) != -1 &amp;amp;&amp;amp; xpathSample.indexOf(&amp;#39;]&amp;#39;) != -1)&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;xpathSample = xpathSample.replace(xpathSample.substring(xpathSample.indexOf(&amp;#39;[&amp;#39;),&amp;nbsp; xpathSample.indexOf(&amp;#39;]&amp;#39;) + 1), &amp;quot;&amp;quot;);&lt;br /&gt;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;var nodeSample = domSample.selectSingleNode(xpathSample);&lt;br /&gt;&amp;nbsp;if(nodeSample == null) throw new Error(&amp;quot;Unable to insert node: &amp;#39;&amp;quot; + xpathSample + &amp;quot;&amp;#39; doesn&amp;#39;t exist in sampledata.&amp;quot;);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;//Get reference to parentnode&amp;nbsp;&lt;br /&gt;&amp;nbsp;var xpathParent = xpath.slice(0, xpath.lastIndexOf(&amp;#39;/&amp;#39;));&lt;br /&gt;&amp;nbsp;var nodeParent = XDocument.DOM.selectSingleNode(xpathParent); &lt;br /&gt;&amp;nbsp;if(nodeParent == null) throw new Error(&amp;quot;Unable to insert node: Parent &amp;#39;&amp;quot; + xpathParent + &amp;quot;&amp;#39; doesn&amp;#39;t exist.&amp;quot;);&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;//Determine if the parentnode has a sibling that can be used as a reference&lt;br /&gt;&amp;nbsp;var nodeRef = null;&lt;br /&gt;&amp;nbsp;var nodeSampleRef = nodeSample.nextSibling;&lt;br /&gt;&amp;nbsp;while(nodeSampleRef != null &amp;amp;&amp;amp; nodeSample.prefix == nodeSampleRef.prefix)&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;nodeRef = XDocument.DOM.selectSingleNode(xpathParent + &amp;quot;/&amp;quot; +&amp;nbsp; nodeSampleRef.nodeName);&lt;br /&gt;&amp;nbsp;&amp;nbsp;if(nodeRef != null) break;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;nodeSampleRef = nodeSampleRef.nextSibling;&lt;br /&gt;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;//Insert the node&lt;br /&gt;&amp;nbsp;try&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;nodeParent.insertBefore(nodeSample, nodeRef);&lt;br /&gt;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;catch(e)&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;throw new Error(&amp;quot;Error inserting node. Are you trying to insert an already existing node while it is non-repeating?&amp;quot;);&lt;br /&gt;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;//Example&lt;br /&gt;function btnExample::OnClick(eventObj)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;try&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;InsertNode(&amp;quot;/my:root/my:repeating_group&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;InsertNode(&amp;quot;/my:root/my:repeating_group[3]/my:other_repeating_group&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;InsertNode(&amp;quot;/my:root/my:repeating_group[my:field1 = &amp;quot;InfoPath&amp;quot;]/my:group_that_is_not_in_default_values&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;InsertNode(&amp;quot;/my:root/my:group/my:group_that_is_not_in_default_values&amp;quot;);&lt;br /&gt;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;catch(e)&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;XDocument.UI.Alert(e.message);&lt;br /&gt;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve tested a bit in InfoPath 2007, but might be possible still some errors occur in specific situations. Let me know.&lt;/p&gt;
&lt;p&gt;Albert&lt;/p&gt;
&lt;p&gt;edit: forgot to metion you can&amp;#39;t use [ and&amp;nbsp;] inside the predicates right now. And it might be a good idea to return the newly inserted node. If i can find some time i might take care of both.&lt;/p&gt;</description></item></channel></rss>