To continue the series on umbraco Contour, in this post I’ll show you how you can use the data sources feature in Umbraco Contour. When you navigate to the Contour section (if you have Contour installed, otherwise you can install it from the package repository, developer section/ packages / package repo / umbraco pro) you’ll notice 3 trees in the treeview on the left. Forms, Data Sources and Prevalue Sources.
Data Sources allow you to create a form based on a Data source and submit your form entries to that data source. By default there are 3 types of data sources
- Save as node (create form based on document type and create an umbraco content document on submit)
- SQL Database (create form based on database table and submit data to that table)
- Webservice (create form based on webservice and post to that service on submit)
In this post I’ll use the SQL Database data source type.
I have the following database that contains a subscription table with some sample fields including a title field which is a foreign key of a title table.
So now I want to add a form to my site that submits records (subscriptions) to this table. Using data sources in Contour you can do this without a single line of code, just by completing a simple wizard.
The first step is to create a new datasource (so simply right click the data sources tree and select create and then provide the new data source a name). Once the data source is created the next step is to select the type, in this case it will be ‘SQL Database’
Once the type is selected the settings for the specific type should load. In case of the ‘SQL Database’ type there are 2 settings.
- Connection string
- Table name
Once these are setup correctly and the datasource is saved you should get a ‘create form’ button, this will start the form creation wizard.
In the first step of the wizard you’ll need to select which db columns you want to include on the created form (if the db columns doesn’t allow nulls this will be autu checked).
Depending on the presence of foreign keys the next step will be to setup the foreign keys, so you’ll have to select which column will be used to display
And the final step allows you to map a fieldtype to each of the fields that will be created
Once that is setup simply hit the ‘save form’ button and a new form will be created
Since the form is linked to a data source you’ll have limited editing possibilities
That’s it now we have a form that will submit data to a custom database table, without writing a single line of code but by simply completing a 3 step wizard.
And it’s of course also possible to extend these data sources by creating your own, check out the contour shared sourcecode and take a look at the default data sources code.