Dynamically modifying udcx connection files

In a previous post I discussed how to publish InfoPath forms with code behind as a feature in SharePoint 2010. This approached had another side issue to handle; How to save/read the data to/from SP list while you don’t know the URL of the site you’re deploying to. In my case all my InfoPath form were supposed to read from a REST service that I deploy in my feature package. My approach to solve this issue is

  • Create a Data Connections Library and deploy it in the feature.
  • Create a udcx file that is deployed by default to this library with no URL in the connection Info element.
  • Use the code in the event receiver to modify it dynamically based on the deployment URL.

So to get started:

  • Add new item, and choose list instance. The Elements.xml should like something like this
  • Now add a new module to the project. Its Elemnts.xml should be like this
  • The above code essentially tells SharePoint to deploy a file inside the module’s folder called DataService.udcx and fills out its properties -e.g. description, connection type, etc…- and point it to the DataConLibrary we created earlier.
  • Now add a new item to the module and choose it to be an XMl file. Rename it to DataService.udcx in the project explorer, then edit it and insert the following code
  • Great! Now we almost have everything in order. Now we’ll have a list, and a module that automatically deploys a udcx file to the list and populates its properties. Only one issue, the udcx points to a DataService.svc without specifying its full URL. To achieve this we have to add the following code to the event receiver

  • The above code essentially does the following
    • Get the connection file
    • Create a temp file and writes the default udcx file to it
    • modify the temp file and insert the url of the feature’s parent site -since this features scope is Site the parent is casted as SPSite- into the appropriate location -which is the SelectCommand/ConnectionInfo/Query XMl element-.
    • Finally write the temp file back to the original SPFile in the data connections library

And that’s just about it! This way you can deploy multiple udcx files that points only to the names of your lists, and at the time of deployment you can modify the url to be the absolute path to your service -or library or whatever you want to save/read the InfoPath data to/from-. In case you want the udcx file to save you’d have to update the query element inside the UpdateCommand element instead of the SelectElement. There are tons of articles and posts dealing with how to consume data connection files in InfoPath forms so I won’t bother talking about it.

There is an alternative approach and to be honest I don’t remember why I didn’t choose to venture this way as it’s been a long time since I was involved in this project. The alternate approach is to use FormsService.DataConnectionFiles.Add() method to add the data connection to Central Admin like we did with InfoPath forms in the last post. This way in InfoPath you can choose the data connection to be centrally managed.

This is the second shortcut I took in SharePoint. Got any comment, ideas, or even a better way to tackle this issue, I’d really love to hear about it.