How to add predefined values to the backend create view of Dynamic content items

How to add predefined values to the backend create view of Dynamic content items

December 15, 2014 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

 An interesting request we received form a client last week was to add predefined values to some of the dynamic content item properties on creation. This could help content authors to avoid typing the same content over and over again on each newly created item, adding some predefined formulas, etc.

Currently when you click on Create new item in the backend Sitefinity creates an empty item of that type, and binds the Field Controls in the DetailForView to the empty item, thus facilitating the input of the desired property values form the user, though each FieldControl's UI. Since we're creating a blank item every time you click on create new, it's not possible to use some predefined values, however Sitefinity provides infrastructure to add an additional logic to the backend using extensions scripts, so we can access an item and accomplish our task of settings a value to a particular FieldControl of our choice.

Let me elaborate quickly on the approach we're taking. Each module in Sitefinity can have create/edit/list views in the backend grid. Each view is built dynamically when you open it, reading the values from the module definitions. What this means, in a nutshell, is that Sitefinity checks what fields your module has, and renders the FieldControl that provides the UI for manipulating with this particular property.

All of this is done client-side, by a control called DetailFormView. This is one of our most complex controls as it accommodates the create/edit functionality for all content types in Sitefinity. As such we have exposed certain extensibility points which we can benefit from, and  plug some custom logic. This is done via an Extension script. In the extension script we can hook up to a particular event thrown by the DetailFormView and work with the items we get at this point.

What we wish to achieve is the following:


In our case I've created an Extension script where I'm subscribing to the formCreated event - it's thrown once the DetailFormView is constructed, and all FieldControls have been bound to the blank data item which we create each time you click on the "Create new" button. Once the event fires I can get each field control on the DetailFormView and check for its FieldName. Based on the FieldName I can read some dictionary of predefined values and check if there's a  record for this FieldName. if there is one, I'm setting it as the current value of  the FieldControl. Here's the implementation of this spike:

// called by the MasterGridView when it is loaded
function OnDetailViewLoaded(sender, args) {
    // the sender here is DetailFormView
//This event is fired once the Detail Form is created and field controls have been bound top the empty item
function formCreatedHandler(sender, args) {
    var detailFormView = sender;
    var fieldControlIds = detailFormView._fieldControlIds;
    for (var i = 0, length = fieldControlIds.length; i < length; i++) {
        var control = $find(fieldControlIds[i]);
        if (control) {
            if (defaultValuesArray[control.get_title()])
var defaultValuesArray = new Array();
defaultValuesArray["Title"] = "Test default title";
defaultValuesArray["Description"] = "Some default description";

As you can notice for the sake of this sample I've added a global variable that holds the default values. You'll need to adapt it to suit your requirements, by using the name of your field as the array key and set the desired default value.

In order to tell Sitefinity that it should use your Extension script you need to go to Administration -> Settings -> Advanced -> ContentView ->  Controls -> Here you need to find your module and expand it. Then in the module's structure go to Views -> YourModuleNameBackendInsertView - this is the definition of the view Sitefinity shows when you create a new item. You can then go to Scripts and add a new script there by specifying:
Script location - this is the relative path to the extension script file in your project
Name of the load method - here you should type OnDetailViewLoaded

Save the changes and Sitefinity should be using the extension script for the create new item mode of your module.

You can create different extension scripts and configure them following the above described procedure for each particular module.

NOTE: Please keep in mind that if you change the module structure (i.e. go to Administration ->  Module builder -> Your module and change something there e.g. delete a field, and then Save changes) the extension script configuration will be lost, and you will need to go back to Advanced settings and configure it following the above procedure. This is so because Sitefinity regenerates all definitions for the module when you click on Save Changes for this module, and since the extension script is not a default value there is no way for Sitefinity to know about it.

To wrap it up, please find here a short demonstrative video I've recorded for you which demonstrates the process of applying the extension script to a local project and the effects of it. In addition you can find attached to this reply the sample extension script file I've used int he video - you can directly include it in your project and modify it to suit your particular requirements.

Vassil Vassilev

View all posts from Vassil Vassilev on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments are disabled in preview mode.
Latest Stories in
Your Inbox
More From Progress
The New Mobile Development Landscape
Download Whitepaper
IDC Spotlight Sitefinity Thumbnail
Choosing the Right Digital Experience Platform to Improve Business Outcomes
Download Whitepaper
The Fastest Way to Build Mobile Apps With Cloud Data
Watch Webinar