InfoPath Init Cycle and Dynamic Endpoint urls
I've created a InfoPath form designed to be highly movable. Somewhat against my own principles these forms have more than a few dataconnections and I wanted the reconfiguration effort to be minimal, preferrably zero, when the form was moved from one Sharepoint site to the next.
It proved quite difficult to have InfoPath make lookups to relative urls, and for now I ended up with a strategy of storing endpoint configuration in a Sharepoint list, and using the values in this list as a basis for configuring the rest of the data connections on the form. In theory it's possible to make this solution completely site relative by getting the Sharepoint Url from the infopath form and then querying the Sharepoint configuration list through a regular WebServiceAdapter. However, I had a bit of trouble getting the regular webservice adapter towards the sharepoint list to work, so I ended up using a SharepointListAdapter and manually setting the source for this adapter only.
This is how I retrieve the endpoint for the webservice used in the form from the pre-defined SharepointListAdapter:
When the endpoint URL is retrieved I use this function to loop all my DataAdapters and replacing any original value with the configured endpoint url:
Originally I configured the secondary data connections to query on startup and lived happily until I started moving the form. I realized that my logic for changing the endpoint url's for my secondary data sources was not called before the form queried them. In my ASP.NET head I was thinking OnInit when I really was coding in OnLoad.
I investigated the Init cycle of the form and figured that the load events are called in this sequence:
- Querying dataconnections configured to be queried on startup
- Executing startup rules
- The OnLoad event
In order to get my movable form going I had to be able to call script (to reconfigure urls) before querying data connections, thus I cannot use neither autoquery nor startup rules. More script code then..
Thinking back to a while ago when I designed the service contract for this form I have to admit I made a major error. My current granularity is too low, and the form main datasource should have included most of my secondary datasources. The service contract is crucial to beeing able to create efficient clients. As usual I'm learning the hard way.
UPDATE: Ian takes a more deployment focused approach to make datasources dynamic.