Using Event Receiver to Set Default Values for Publishing Pages
While ago I posted something very similar to this. Here
Here’s yet another solution, and way better and faster to implement.
First create web scoped feature, that adds ItemAdded event receiver to Pages library (should’t be a task). In the ItemAdded method, set the default values which are defined in the ContentType, to each field in the listItem.
{
foreach (SPField field in properties.ListItem.ContentType.Fields)
{
if(field.DefaultValueTyped != null)
properties.ListItem[field.Id] = field.DefaultValueTyped;
}
properties.ListItem.SystemUpdate();
}
The code loops all the fields in the current Listitems ContenType and then checks if the default value is set. If it is, it will add it to the listitem.
The default values are typed, so no casting needed.
The drawback, the page might load faster than the items are updated and you will end up with empty fields again, refreashing will help but thats notthe way to go. If using itemAdding event all properties are embty cos the listitem is not created yet. It’s possible to set the values, but then you should know what’s the contentype going to be. At leat I didnt find if from nowhere… Btw, all this is fixed in the SharePoint 2010..
***********************************************
Jerry Seinfeld: Why not? We’re neighbors. What’s mine is yours.
Cosmo Kramer: [leaning against the door-frame and looking around in wonder] Really?
***********************************************
Popularity: 5% [?]