Hi,
recently while passing through the Microsoft forum threads I found an interesting request coming from a forum member. He wanted to change the default values of a form field depending on the current content type selected. This works as long you use different fields with different default values in your content types. It becomes however a problem if you share common columns and want to have different default values.
Therefore, I was looking for a solution to this problem and found out that the LFM framework could be used to achieve this task quite simply. You can find more information about this tool on the official codeplex page.
A small demo
Imagine we have a custom list “CustomList” with two content types associated:

“Item” and “Task” have the column “Title” in common. You could define a default value for the “Title” column, but this default value is the same for both, “Item” and “Task”.
In our scenario, however, we would like to have completely different default values for this column. Imagine we would like to have the “Title” column of an “Item” set to “This is a new item”. On the other hand, we would like to add the default value to the “Title” column of a “Task” to “This is another task”.
To accomplish this we need to differentiate the content types when a form is opened. The form could be opened in these ways when working with a list. (Before you start complaining about my bad picture I can only say that: I am a computer scientist and not a designer
).

When a user clicks on 1, 2 and 4, the NewForm.aspx with the default content type (in our case the “Item”) is opened. Button 3 opens the NewForm.aspx with the “Task” content type associated.
I noticed that you can find out through JavaScript what is the current associated content type by following these rules:
- When a user clicks on 1 and 4, the NewForm.aspx is opened and no additional information is shown
- When a user clicks on 2 and 3, the NewForm.aspx is opened and an additional parameter “ctype” is passed in the url as get parameter
And exactly the “ctype” assigned saves us from banging our head against the wall. With this information we can add some code to our LFM framework to assign different default values when different content types were selected for creating a new item. Please find the code snippet below. Add this code to the “LFMConfig.js” of the LFM framework:
$(document).ready(function () {
// the ctype id of the content types associated to the "CustomList". Change these ids accordingly
// you can find them in the URL when clicking on the Content Type under List Settings
var ItemContentId = "0x01008068822D1BEA4D4CA1EAE00773348A03";
var taskContentId = "0x010800B9DDBA5F57BAEE4A8BF98D065412E690";
// verify if this is the customNewForm. Change the URL accordingly.
var isCustomNewForm = window.location.href.indexOf("/Lists/CustomList/NewForm.aspx") > -1;
// add the non default content types first
if (isCustomNewForm && window.location.href.indexOf(taskContentId) > 1) {
var title = new qppfSPFormField("Title", "SPFieldText");
title.setValue("This is another task");
// add your default values here for the Task
// if all non default content types were not found, then it should be the default content type
} else if (isCustomNewForm) {
// add your default values here for the Item
var title = new qppfSPFormField("Title", "SPFieldText");
title.setValue("This is a new item");
}
});
The code snippet is pretty straightforward. We use the LFM framework to differentiate which content type is currently selected in our “NewForm.aspx” by verifying following:
- if it is the CustomNewForm and the URL contains the id of the Task content type associated then it must be the “Task”
- all other combinations are related to the “Item” content type (which is our default content type)
if you want more content types, then add the if statements always before the default content type branch
you can get the associated list content type id in the URL when clicking on the content type under the list settings

The rest is only a standard usage of the LFM framework. The final result looks as follows (first picture when clicking on the default content type. Second picture when clicking on the Task content type).


Summary
In this small example I showed how the LFM framework can be used to manipulate default values in a NewForm.aspx depending on the current content types selected. You can find more information about this tool on the official codeplex page.
Stay tunes,
Patrick