This project is read-only.

Extending Behaviour - Default Templates

Topics: Developer Forum
Apr 1, 2009 at 10:21 PM
Hi all,

I've puzzled over the documentation today at work and I'm having difficulty with a few things in N2:

I've extended the Event Item to give it an end date, I added the properties/decoratation/attributes etc.. in my new EventItem class; when I add a new Item mine overides the existing Event (which is good), I get the date picker etc for end date after the normal date.
When I save this, I checked the database - the data seemed to be there. However when I edit the item I get an empy date field, I've also started getting the message: There is a newer version of this item that hasn't been published. Edit the newer version »


Also, I cannot seem to get the new EventItem class to use the aspx file I've created in my UI folder of the template project.. it always seems to use the one in N2 (calendarItem.aspx) grrrrr..

Please help!


Apr 1, 2009 at 10:53 PM
Can you post some code?
Apr 1, 2009 at 11:01 PM
Hey, of course:

[Definition( "Life Event", "Event", "A new event item", "", 110 )]
    [DefaultTemplate( "Event" )]
    public class EventsItem : Event
    {
        [Editable( "Event End Date", typeof( N2.Web.UI.WebControls.DatePicker ), "SelectedDate", 23, ContainerName = Tabs.Content )]
        public virtual DateTime? EventEndDate
        {
            get { return (DateTime?)GetDetail( "EventEndDate" ); }
            set { SetDetail( "EventEndDate", value ); }
        }

        public virtual string EventEndDateString
        {
            get
            {
                if ( !EventEndDate.HasValue ) return null;
                if ( EventEndDate.Value.TimeOfDay.TotalSeconds == 0 ) return EventEndDate.Value.ToShortDateString();

                return EventEndDate.Value.ToString();
            }
        }
    }

Then I have created an Event.aspx file which is currently in UI\Views (but I can move it back to just \UI - both haven't worked). This simply contains the same code as the CalendarItem.aspx in the source with something extra to show it's using my code:

<asp:Content ID="cc" ContentPlaceHolderID="TextContent" runat="server">
    <n2:EditableDisplay PropertyName="Title" runat="server" />
    <span class="date"><%= CurrentItem.EventDateString %></span>
    END:<span class="date"><%= CurrentItem.EventEndDateString %></span>
    <n2:EditableDisplay PropertyName="Introduction" runat="server">
        <HeaderTemplate><p class="introduction"></HeaderTemplate>
        <FooterTemplate></p></FooterTemplate>
    </n2:EditableDisplay>
    <n2:EditableDisplay PropertyName="Text" runat="server" />
</asp:Content>
Apr 1, 2009 at 11:11 PM
Okay, I can see one thing.

Change [DefaultTemplate( "Event" )] into [Template("~/UI/Event.aspx")]. The default attribute defaults to ~/Templates/UI/Views, it it could be a good idea to introduce a convetion based attribute.

As for the date beeing empty I can't spot anything wrong. Is the data there if you edit the newer version?
Apr 1, 2009 at 11:15 PM
Hey, thanks for the reply.. If the attribute defaults to /Templates/UI/Views... shall I just create a new folder called templates in my project and move everything in there? In the mean time I will try changing it as you suggested..

When I try to edit the newer version nothing happens, the page refreshes and the message is still there. :(

Will change and try again just in case! :)
Apr 1, 2009 at 11:19 PM
Apologies, the message changes to edit the master version.. but nothing is different about the page!

As for the [Template] adding the change you suggested and building the project doesn't seem to fix it either.. I still get the CalendarItem aspx file as far as I can tell.
Apr 2, 2009 at 9:39 AM
I cannot for the life of me, figure out how I can overide the template that is being used by my new Event Item.. I've tried:

I showed the templates folder in my project, then moved my Event.aspx (and code behind) files into there.

[DefaultTemplate( "Event" )]
[Template("~/Templates/UI/Views/Event")]

Also tried using the override for templateUrl:

public override string TemplateUrl
        {
            get
            {
                return "~/Templates/UI/Views/Event.aspx";
            }
        }

Still no joy, it uses the local projects CalendarItem.aspx file which I guess is set in the N2 source for an Event item  (event.cs) has this:
[DefaultTemplate("CalendarItem")]

Is it that I cannot override this? Everytime I think I understand something about N2 I run into another problem :(
Apr 2, 2009 at 10:02 PM
Okay, next idea: Is it really reeeeally a EventsItem item? If you look in the database, is the discriminator "EventsItem"?

You could also try overriding FindPath and put a breakpoint there.

Also make sure that you don't mix the DefaultTemplate and Template attributes. What could happen is that defaulttemplate executes first.

Also visit /edit/install/diagnose.aspx. The latest version features a new column displaying templates serving each type of item.

It might be possible that these attributes are not sorted the way I expected. Please report the order of the attributes in the template column of the diagnostic page.
Apr 3, 2009 at 8:11 AM
Just a reminder (not sure this is exactly what could solve your problem, but sounds familiar), if you want to modify a template of a stadard N2 (N2.Templates) items without changing original sources, you can go with ASP.Net Adapters -- they're somewhat tricky from a first glance, but with them you can considerably alter the behavior of original items. Good news is that you can develop them independently and assign to a desired item types via .browser file.
Apr 3, 2009 at 9:25 AM
Edited Apr 3, 2009 at 12:29 PM
Hey,

I checked the dbo.n2Item table (hope that was the right place).. there's nothing in there under EventsItem 'Type'.. just Event hmmmmmn.

Does it have something to do with the definition on the EventsItem class? [Definition( "Life Event", "Event", "A new event item", "", 110 )]

*EDIT* I tried removing the "Event" -> [Definition( "Life Event", "", "A new event item", "", 110 )].. Now I can see two Event types when I try to add a new item to a calendar.. but clicking Life Event just brings up the edit calendar fields (what the heck). The tool tips on the definition don't help really, I'm going googling for answers to the params passed in.

*EDIT EDIT*
I did some perusing of the object browser regarding the Definition attribute.. Changed it to this: [Definition( "Life Event", "Life Event", "A new event item", "", 110 )].. Voila! Now it works a charm! Now if only I could 'turn off' the original Event, I won't want anyone using that in the CMS!

*YET ANOTHER EDIT*p.s. Nothing is available at /edit/install/diagnose.aspx - The resource cannot be found). <- That is becase I removed the install folder. Please forgive me, my head is weary!

Thanks
Pete
May 13, 2009 at 3:15 PM

Going back to this point, it seems that N2 wants to force me to use the Templates folder within N2, even when I ask it to do otherwise...

    [Definition("News Item", "NewsItem", "New 'News' Item", "Create a new 'News' item - this can represent an article.", 1)]
    [Template("~/Addons/News/NewsItem/UI/Views/NewsItemDisplay.aspx")]
    [ReplacesParentDefinition]
    public class NewsItem : Templates.Items.News
    {

    }

For some reason, N2 uses the default of templates/ui/views/newsitem.aspx!! It shows in the DB as a "newsitem" (rather than the "news" which is the default). What's doing on? Why can't I override this?

May 13, 2009 at 8:46 PM

I tried your code and it seems to work (I getting resource cannot be found /Addons/News/NewsItem/UI/Views/NewsItemDisplay.aspx).

Are you sure there isn't an old news that is the one you're really seeing. Old types are not removed when using the replace definition.

May 14, 2009 at 11:42 AM

Well when I try to create a new page in the CMS menu, my version which should be replacing the old one doesn't appear.. See this example below, I want to override the Calendar object in order to handle my own display.   

     [Definition("Event Item Container", "EventContainerItem", "A list of Events. Event items can be added to this page.", "", 150)]
    [RestrictParents(typeof(IStructuralPage))]
    [Template("~/Addons/Events/EventContainerItem/UI/Views/EventListDisplay.aspx")]
    [ReplacesParentDefinition]
    public class EventContainerItem : Calendar
    {
       
    }

Now when you tried to create a new page, in the list where it used to say: "Calendar Page - A list of recent events" surely it should now say: Event Item Container - A list of Events. Event items can be added to this page".

However, it doesn't do this.. as if N2 is ignoring me trying to override the Calendar item. Why?

May 14, 2009 at 12:08 PM

Now it seems after building, rebuilding etc over and over.. N2 has changed it over! Arrghhhh.. Guess everyone can ignore that post.