Class names causing duplicate import.

Topics: Developer Forum
Apr 3, 2009 at 11:05 AM
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
        {
            get
            {
                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!
peteski
Coordinator
Apr 4, 2009 at 10: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:

    [Definition]
    [ReplacesParentDefinition]
    public class DefinitionReplacement : DefinitionReplaced
    {
    }

It's also possible to replace multiple items:

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

Apr 6, 2009 at 10: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!
Coordinator
Apr 6, 2009 at 6:44 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.