Use dynamically created InfoPath form instance to pre-fill form
If you want to pre-fill InfoPath form before user gets his/her hands to it... you have two ways to do that:
- Call Web Services from InfoPath
- It normally takes quite long
- Dynamically create InfoPath form instance
- Fast and there isn't any limitations where to get data
Both methods have been mentioned many times in different sites but I still decided to create a demo about the "Dynamically create InfoPath form instance"to make this crystal clear... and give some code to get you started on this if you're interested.
So I created really simple InfoPath Request Form:
I added submit rule to the button (I just used fixed url for the form library).
Then I created following UI for my solution:
Here is the code behind the astonishing UI (a.k.a. button inside web part):
|
|
In lines 54 to 73 I created the XML for the form (it looks like real mess and I'm sorry about it!). Most important stuff happens between lines 66 and 72 where the actual field values are stored. Of course you could use some magic code to get the correct values to the variable at lines 47 - 52. And you can also probably see the URL of the XSN file (line 57) and as well as the namespace (line 63). It looks like this when user clicks the Create request button:
And when user presses the button in InfoPath the form data is submitted to the form library:
So hopefully after this post you'll remember that this method is powerful (and not the messy XML :-) if you want fill InfoPath with data. You have much more possibilities than just simple web service request in InfoPath.
By the way... Why do I always end up with titles with word "dynamic*" in it :-) (see my previous post for example)
Anyways... Happy hacking!
J
Comments
Anonymous
November 07, 2007
Nice post.. but in a real world you probably want to publish the infopath form to MOSS more than once. With each publish the timestamp in the namespace changes, doesn't it? So, is there a nice way to retreive the new namespace, other than blowing up the xsn-file and start digging? It would be cleaner to retrieve an empty form from Forms Services with correct namespace and everything. But how can I do that in code?Anonymous
November 07, 2007
Hi Jari! If you do it the correct way => don't start from InfoPath :-) Then you actually define your namespace yourself. The problem is that people normally start by launching InfoPath... and don't think about the schema. But you should first create schema and then start building your form on top of it. Then your namespace stays as you have defined. Anyways... Happy hacking! JAnonymous
November 07, 2007
Hmm.. you're right. My form is imported from excel, so the schema is a bit reverse engineered.. But now when I have version 1.0 ready I can get the schema out, fix the namepace and continue from there. Cheers!Anonymous
November 08, 2007
Holy Cow dude :) Do you ever sleep ? what ever pills you are taking, i want those too :)) :)Anonymous
November 08, 2007
Anpassung Liam Cleary MOSS2007 – Link Lists and the Content Query Web part Adding "OpenInNewWindow" optionAnonymous
November 28, 2007
The comment has been removedAnonymous
June 20, 2008
I tried the about example with an aspx page. However, when I try running it, I get an error saying: The element '{http://schemas.microsoft.com/office/infopath/Test5/myXSD/2008-06-20T18:52:11}myFields' is used but not declared in the DTD/Schema. As you can see, I have changed the lines: 66, 72 , 47 - 52 and 57, 63. Can you advise, what is wrong, or can you kindly post your code for me to donwload.Anonymous
July 15, 2008
The comment has been removedAnonymous
July 21, 2008
Hi, Is it possible to fill submitted infopath form clicking on it and filling data and submit it? If Yes then How? Thanks in advanceAnonymous
October 17, 2008
Love your blogs. Can't print them out. The first Page comes out right The rest don't print. Any suggestions. thanks, Stephan