Tip #73: How to update InfoPath DataSource dynamically

Rate this item
(0 votes)

Developing SharePoint applications that use browser-enabled InfoPath forms requires a lot of interactions with the Web Services and it's usually done via DataSources properties of the InfoPath. You store all your connections to the Web Service into the DataSources and uses properties name to communicate.

The tricky situation of DataSources is in deployment. When you compile and publish InfoPath form it includes the connection settings that you configured locally, and the InfoPath form wont work when you deploy in to another box (UAT or Production) where you services and server names have different names and paths.

The solution is to update the DataSource url dynamically. Posting here the code-snipped I used across several projects. The idea to move the service name to the SharePoint web.config and update DataSources host name by checking if the property contains your service name of "_vti_bin" for the OOTB Web Services.

Code Snippet
  1. public static void UpdateDataSourceURL(DataConnectionCollection dataConnections)
  2. {
  3. if (SPContext.Current != null)
  4. {
  5. var webServiceURL = WebConfigurationManager.AppSettings["<custom_Web_Sever_Path>"];
  6. var siteURL = WebConfigurationManager.AppSettings["<website_url>"];
  7. foreach (var connection in dataConnections)
  8. {
  9. if (connection is WebServiceConnection)
  10. {
  11. var dataConnection = (connection as WebServiceConnection);
  12. Uri webservicePath;
  13. if (dataConnection.ServiceUrl.ToString().Contains("_vti_"))
  14. {
  15. string url = string.Format("{0}{1}{2}", siteURL, dataConnection.ServiceUrl.Segments[2], dataConnection.ServiceUrl.Segments[3]);
  16. webservicePath = new Uri(url);
  17. }
  18. else
  19. {
  20. webservicePath = new Uri(webServiceURL + dataConnection.ServiceUrl.Segments[2]);
  21. }
  22. dataConnection.ServiceUrl = webservicePath;
  23. }
  24. }
  25. }
  26. }



Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.