Search the web
Sign In
New User? Sign Up
sitecore
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Sitecore 5.3 - Default Field value to Today's Date   Message List  
Reply | Forward Message #3279 of 3315 |
Re: [sitecore] Re: Sitecore 5.3 - Default Field value to Today's Date

One more thing I forgot. Sitecore automatically stores the version's creation
date in a field named __created (Sitecore.FieldIDs.Created). This may or may not
already be what you need. I think if you need the item creation date rather than
the language version, or if the user needs to specify the creation date instead
of using the actual creation date, then you need a new field.

----- Original Message ----
From: John West <johnpwest3@...>
To: sitecore@yahoogroups.com
Sent: Tuesday, January 15, 2008 5:44:24 PM
Subject: [sitecore] Re: Sitecore 5.3 - Default Field value to Today's Date


Hello,

This forum has basically been abandoned in favor of the SDN forums:

http://sdn5.sitecore.net/Forum.aspx

This specific question is probably best addressed on the Developer
Talk > General Discussion forum:

http://sdn5.sitecore.net/forum//ShowForum.aspx?ForumID=10

I believe you could do this with nvelocity:

http://sdn5.sitecore.net/Articles/API/Using%20NVelocity.aspx

But I haven't tried and people seem to have had issues with this
approach:

http://sdn5.sitecore.net/forum//ShowPost.aspx?PostID=6849

I would probably add an event handler for the item:saved event,
especially since you will probably end up with other save logic. You
could also do this with a pipeline:

http://sdn5.sitecore.net/forum//ShowPost.aspx?PostID=6227

Assuming an event handler, the logic would be something like:

If the item being saved is based on one of your templates
and the save is happening in the master database
and the value of that field is empty
then default the value to today's date

Information on events is here:

http://sdn5.sitecore.net/Articles/API/Using%20Events.aspx

Just to make it more complicated, updating data from within a save
handler can raise save events, which could cause infinite recursion.
This is generally resolved by adding a static hashtable to the handler
class:

private static Hashtable _htItemsInProcess;

public MyHandler()
{
_htItemsInProcess = new Hashtable();
}

The actual code to set the value would probably look something like
this:

Sitecore.Data.Items.Item itm =
Sitecore.Events.Event.ExtractParameter(args, 0);

if ( ! _htItemsInProcess.ContainsKey( itm.ID ))
{
htItemsInProcess[itm.ID] = itm;

if ( itm.Database.Name.ToLower() == "master" &&
itm.Fields["myfield"] != null )
{
itm.Editing.BeginEdit();
itm.Fields["myfield"].Value =
Sitecore.DateUtil.ToIsoDate(DateTime.Now);
itm.Editing.EndEdit();
}

htItemsInProcess.Remove( itm.ID );
}




Yahoo! Groups Links









________________________________________________________________________________\
____
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now.
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ




Wed Jan 16, 2008 2:05 pm

johnpwest3
Offline Offline
Send Email Send Email

Forward
Message #3279 of 3315 |
Expand Messages Author Sort by Date

Hey All, I am new to Sitecore and I am curious on how to set the default value of a field to Today's Date. Thanks in advance, Wayne...
dice55m
Online Now Send Email
Jan 16, 2008
12:47 am

Hello, This forum has basically been abandoned in favor of the SDN forums: http://sdn5.sitecore.net/Forum.aspx This specific question is probably best...
John West
johnpwest3
Offline Send Email
Jan 16, 2008
1:45 am

Oops. if ( itm.Database.Name.ToLower() == "master" && itm.Fields["myfield"] != null ) That should include checking that the field doesn't already have a value:...
John West
johnpwest3
Offline Send Email
Jan 16, 2008
1:55 am

One more thing I forgot. Sitecore automatically stores the version's creation date in a field named __created (Sitecore.FieldIDs.Created). This may or may not...
John West
johnpwest3
Offline Send Email
Jan 16, 2008
4:01 pm

OK, now I'm even annoying myself. I think you could use item:created instead of item:saved, which should not raise item:created, which should mean you don't...
John West
johnpwest3
Offline Send Email
Jan 16, 2008
4:13 pm

... Appreciate the quick response John. I created the item:created event handler and it works almost perfectly. When I create an item, it successfully...
dice55m
Online Now Send Email
Jan 16, 2008
9:11 pm

That is very weird - I have never seen that before. It could be something else you're working on, or you might try item:saved instead....
John West
johnpwest3
Offline Send Email
Jan 16, 2008
9:15 pm

Does it open the same item in all content editors, or different items (maybe this is a result of default children in your master?). You may want to use...
John West
johnpwest3
Offline Send Email
Jan 16, 2008
11:02 pm

Hey John, The item is in all of the newly opened Content Editors, with no data tree (the site tree), just the lone item. This is just weird, I do have a...
dice55m
Online Now Send Email
Jan 19, 2008
12:33 am

Sorry for the delayed response. Can you explain what you mean by an OnSave event handler? Do you mean an item:saving or item:saved event handler, a processor...
John West
johnpwest3
Offline Send Email
Jan 21, 2008
5:48 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help