Using Event Receiver to Set Default Values for Publishing Pages

March 29 2010 17 comments

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.

public override void ItemAdded(SPItemEventProperties properties)
 foreach (SPField field in properties.ListItem.ContentType.Fields)
  if(field.DefaultValueTyped != null)
   properties.ListItem[field.Id] =  field.DefaultValueTyped;

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: 6% [?]

17 comments to “Using Event Receiver to Set Default Values for Publishing Pages”

  1. I like the valuable information you supply for your articles. I’ll bookmark your blog and test once more here regularly. I am somewhat certain I’ll learn many new stuff right right here! Best of luck for the following!

  2. Hi i am kavin, its my first occasion to commenting
    anywhere, when i read this post i thought i could also make comment due to this brilliant piece of writing.

  3. I am actually happy to glance at this webpage posts which includes
    plenty of useful data, thanks for providing these kinds of statistics.

  4. Do you mind if I quote a few of your articles as long as I provide credit and sources back
    to your website? My blog site is in the very same niche as yours and my visitors would
    certainly benefit from a lot of the information you
    present here. Please let me know if this okay with you.

  5. El masaje anatómico es un masaje sensitivo, de toque suave, ideal para relajarse y aliviar tensiones.

  6. They do work very well – but I haven’t washed them yet.Good

  7. swms says:

    This article offers clear idea in favor of the new visitors of blogging, that actually how to do

  8. I am genuinely glad to glance at this website
    posts which contains tons of helpful data, thanks for providing
    these kinds of information.

  9. Kellye says:

    If you are going for best contents like myself, simply
    pay a visit this web site everyday as it provides feature contents, thanks

  10. However a rough guideline has been presented below:
    . As diesel fuel is injected for combustion, fuel ignites as it comes in contact with the compressed air.
    Airsoft electric pistols offer a number of advantages over either spring or gas pistols.
    Air filter: check that the air intake filter is clean. It is vital to choose
    your manufacturing company well, as this would not
    only translate into you acquiring a quality product, but also ensure
    that the machine is maintained to its full capacity for years to come.
    On the other hand, electric motors can easily trip the circuits which have to
    be restored. Besides all these, they supply precision engineered SANPAR Micro filters and
    Air Conditioner. Gary Allen is the co-founder of All Cape Spring Co.

  11. Every time I was experiencing a malfunction or couldn’t know how to use the next, out came the manual and my problems were solved.
    Large collection of stitches, covering quilting stitches, decorative stitches, heirloom and utility stitches for mending and garment construction. People love
    to be admired regardless of what age they are and by achieving this sort of work they virtually guarantee some kind of compliment at one time or another.

  12. However, the success credit should be shared with the applications
    made for android phones because those apps have attracted the buyers towards it.
    From great Android bubble games to shooters there
    is something here for everyone and many of them are free.
    Although this may seem the most logical next step beyond offering games solely online, there has so far been little interest by developers in creating Android ports.

  13. Rosalinda says:

    The best part aboout these games is the fact that youu don’t have to leave your home or as much as your couch to play them wih your child.
    This article looks at the nuts aand bolts of this once popular technology.
    As well as in May, a surge and fire in a plant that made Apple items wiped out
    a couple and hurt greater than a dozen within the town of Chengdu, in southwest China.

  14. hostess says:

    I think the admin of this web site is truly working
    hard in support of his web site, because here every material is quality based data.

  15. Bambi Blanca says:

      Remove of jeweler and blacksmith’s assembly leads to beneath players’ amphitheatre jewelers. Of beforehand alluring doesn’t allegation abundant attention, as continued as you see the interface. There are several enchanting—1h affray weapons enchanting, Diablo 3 gold, 2h affray weapons enchanting, baton enchanting, bow and crossbow enchanting, cuirass enchanting, leg enchanting, gloves enchanting,Cheap wow gold, cossack alluring and rings. Alluring is cher and it costs gold and attenuate abstracts such as blood-soaked anima.  As for Halls of the Dead, all of accomplished players are ‘buried’ here. It is UI function. Through client, we get to apperceive abounding passes. The cartography is accustomed afterwards players abstraction on simulators,Buy wow gold, Diablo 3 gold, such as festering woods, age-old pyre, wortham, abandoned farm, forsaken grounds, cloistral cottage, the old will, fields of misery, arctic highlands and so on. A hero was in Diablo 1. In Diablo 2 he became a NPC. It is not accustomed whether aphotic adventurer will arise in Diablo 3.

  16. Pese a la abundancia de referencias bibliográficas, desde la consolidación de las redes sociales, el interés por el hipertexto ha sido desplazado por otros fenómenos como la participación en comunidades online y la escritura en blogs y wikis, tal y como pone en evidencia la publicación en 2006 de dos títulos como La blogosfera hispana: pioneros de la cultura digital, a cargo José M. Cerezo, Pangea.

  17. I would say the 250 GT California is more elegant than the GTO

Leave a Reply