This project is read-only.

Class names causing duplicate import.

Topics: Developer Forum
Apr 3, 2009 at 12:05 PM
Hey all,

I am trying to create my own versions of some N2 items (News, Events etc.) But want to make it such that the N2 versions don't appear in my CMS..

Obviously when I am creating my own News item, I want to call it News; since that is what it is - and don't want to get bogged down in prepending class names etc. However, despite my News item being in a different namespace (projName.Items vs. N2.Templates.Items) I get an error when trying to run my lovely little news class (see code below).

using System;
using N2;
using N2.Details;
using N2.Templates;
using N2.Web;

namespace Life.Items
    [Definition("News", "RB_News", "New 'News' Item", "Create a new 'News' item - this can represent an article.", 1)]
    [Template( "~/Templates/UI/Views/News.aspx" )]
    public class News : N2.Templates.Items.News
        [Editable( "Date", typeof( N2.Web.UI.WebControls.DatePicker ), "SelectedDate", 23, ContainerName = Tabs.Content )]
        public virtual DateTime? Date
            get { return (DateTime?)GetDetail( "Date" ); }
            set { SetDetail( "Date", value ); }

        public virtual string DateString
                if ( !Date.HasValue ) return null;
                if ( Date.Value.TimeOfDay.TotalSeconds == 0 ) return Date.Value.ToShortDateString();

                return Date.Value.ToString();

Any ideas? Also, anyone know how to 'turn-off' the default N2 items from appearing when creating new items in the CMS?

Thanks very much for any help!
Apr 4, 2009 at 11:20 AM
Sorry, you'll need find another name to your class. I realize the classes in templates should have been prefixed and I'll consider changing this. It's a pretty breaking change however =(

To turn off default items you ca use a nifty little attribute from the n2.definitions namespace:

    public class DefinitionReplacement : DefinitionReplaced

It's also possible to replace multiple items:

    [ReplaceDefinitions(typeof(DefinitionOne), typeof(DefinitionTwo))]
    public class DefinitionReplacesNumbers : N2.ContentItem

Apr 6, 2009 at 11:39 AM
Hey, thanks for the reply.

It's a shame I can't call my classes what they actually are (not the end of the world though).

I'll make use of the code you posted too, that's what I was looking for!
Apr 6, 2009 at 7:44 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.