Developer forum

Forum » Development » Items - Code First

Items - Code First

Morten Bengtson
Reply

Hi,

 

I'm trying to use the Code First approach to create some custom item types. I cannot not find any documentation on this - only the item-based structure preview video.

 

My item types inherit from ItemEntry (Item is sealed) and the item types are created in Dynamicweb when the application loads. However the constructor, Save, Delete, DeserializeFrom and SerializeTo are never called, which means that my item type class is ONLY used for creating item type definition at application load and is not used when creating and editing items of this type?

 

I thought that I could override the methods on my item type class, but apparently thats not the way to do it. Are there any notifications that I can subscribe to?

 

Am I missing something here?

    using Dynamicweb.Content.Items;
    using Dynamicweb.Content.Items.Annotations;
    
    public class Article : ItemEntry
    {
        // Never called.
        public Article()
        {
        }

        public string Headline { get; set; }

        [RichText]
        public string Text { get; set; }

        // Never called.
        public override void Save()
        {            
            base.Save();
        }

        // Never called.
        public override void Delete()
        {
            base.Delete();
        }

        // Never called.
        public override void DeserializeFrom(System.Collections.Generic.IDictionary<string, object> dictionary)
        {
            base.DeserializeFrom(dictionary);
        }

        // Never called.
        public override void SerializeTo(System.Collections.Generic.IDictionary<string, object> dictionary)
        {
            base.SerializeTo(dictionary);
        }
    }

 

 


Replies

 
Morten Bengtson
Reply

To override/manipulate item values I need to subscribe to Page.Saved and Paragraph.Saved and then save the item again? That would work, but it seems a bit clumsy.

 

I also tried implementing a custom field editor (which is based on functionality from Dynamicweb.Analytics?), but that did not work at all.

 

Any help with this would be very much appreciated.

 

 
Pavel Volgarev
Reply
This post has been marked as an answer

Hi Morten,

 

Regarding not being able to override Save/Delete - it's definitely a bug, will try to fix it as soon as possible. Regarding implementing your own editor - you need to inherit from Dynamicweb.Content.Items.Editors.Editor, Dynamicweb.Analytics is for OMC.

 

-- Pavel

Votes for this answer: 1
 
Morten Bengtson
Reply

Hi Pavel,

 

Thanks for your reply. I was kind of hoping it was a bug and not just me being totally incompetent :)

I hope that you will find a fix for this very soon - we need this last week ;-)

 

Regarding the Analytics namespace confusion, I was refering to the ValueConverter property on Dynamicweb.Content.Items.Editors.Editor.

 

The BeginEdit and EndEdit methods on the Editor class had me confused as well. I had to use Reflector to see what was going on and now I think I understand how they are supposed to be used. It would have been a lot more clear to me if instead it was a Render method and a GetValue method taking HttpContext or NameValueCollection as parameter.

 

 
Morten Bengtson
Reply

I have an item class with 6 properties and everything works well. Then I added a GroupAttribute on two of the properties and now my item type only displays the two properties with the GroupAttribute. What am I doing wrong?

 
Morten Bengtson
Reply

And is it possible to make Dynamicweb ignore a property that I use for something else than storing a field value?

 
Vladimir
Reply

Hi Morten!

* add GroupAttribute for other properties too

* Dynamicweb stores only Public properties (but this can be changed by overriding SerializeTo function)

 

Best regards,

Vladimir

 

 

 
Morten Bengtson
Reply

Hi Vladimir,

Thanks. I guess overriding the SerializeTo will be possible when bug 11199 is fixed... looking forward to it :)

 
Pavel Volgarev
Reply

Hi Morten,

 

The thing with the GroupAttribute seems like a bug. At least it worked in early versions when properties without GroupAttribute were added to a default group. Will look into it.

 

-- Pavel

 
Morten Bengtson
Reply

Hi Pavel,

 

Do you know anything about when this will be fixed? I can see that nothing was changed in the hotfix that was released today.

 

I'm not sure if the following is related to the "Save/Delete never called"-bug, but in my template extenders i don't get an instance of my custom item type, but an instance of Dynamicweb.Content.Items.Item, so I cannot access the properties on my item. I prefer to use each property on the class and not just iterate through key/value pairs in the dictionary.

 

The item provided in the template extender should have been an instance of my custom item class, right?

 

Anyway, I found a temporary workaround...

// Create new instance of custom item type and then serialize/deserialize to load it with all the values...
var article = new Article();
var dictionary = new Dictionary<string, object>();
this.Item.SerializeTo(dictionary);
article.DeserializeFrom(dictionary);

// And then I can access the properties...
var headline = article.Headline;
var text = article.Text;

 

 
Kristian Knudsen
Reply

Hi Morten

We are fixing both issues at the moment and will release the changes in a separate hotfix hopefully tomorrow or the beginning of next week.  

 
Morten Bengtson
Reply

Hi Kristian, that sounds great! Thanks for the update.

 

 
Kristian Knudsen
Reply

Hi again Morten

You can now download version 8.2.1.4 from the Downloads section which includes the fix for #11199.

 
Morten Bengtson
Reply

With 8.2.1.4 custom item types does not work at all...

 

Can you please tell me what I am doing wrong here?

 

I have implemented a custom item class, built with reference to DW 8.2.1.4, and there are no other custom extensions:

using Dynamicweb.Content.Items;

public class Article : ItemEntry
{
        public string Headline { get; set; }
}

After deployment and application load, the new item type, "Article", is registered.

 

When I try to create a new item page the dialog for entering the name appears, but when I click "OK" to create the item it will create the page and redirect to /Admin/MyPage/Default.aspx and I am unable to access the edit page and enter a value in the "Headline" field.

 

Requests
200 /Admin/Content/Items/Editing/PageTypeSelect.aspx?cmd=createpage&ParentPageID=43&AreaID=1&BasedOn=Article&PageName=testing
302 /Admin/Content/Items/Editing/ItemEdit.aspx?PageID=44
200 /Admin/MyPage/Default.aspx

 

The page is created in the database with "Article" in PageItemType, but PageItemId is empty.
Current value is incremented in the ItemTypeId table and rows are added in the ItemType_Article table with an Id and NULL in the column for my text field.

 

Trying to create an item on a paragraph gives me this result...

 

 

 


Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 
Line 278: <td class="value"><div><%=Paragraph.ID%></div></td>
Line 279: <td class="label"><div><dw:TranslateLabel ID="TranslateLabel7" Text="Item ID" runat="server" />:</div></td>
Line 280: <td class="value"><div><%=If(Not IsNothing(TargetItemMeta), TargetItem.Id, "")%></div></td>
Line 281: <td class="label"><div><dw:TranslateLabel ID="TranslateLabel8" Text="Edited" runat="server" />:</div></td>
Line 282: <td class="value"><div><%=Paragraph.UpdatedDate.ToString("ddd, dd MMM yyyy HH':'mm", Dynamicweb.Base.GetCulture())%></div></td>

Source File: C:\Projects\Dynamicweb\Application(8.2.1.4)\Admin\Content\ParagraphEdit.aspx Line: 280 

 

 

 

 
Pavel Volgarev
Reply

Ouch, Morten! I'm committing a fix right now. Sorry about that (thought I tested it properly).

 

-- Pavel

 

 

 
Morten Bengtson
Reply

Will this fix be released today? These bugs are making it impossible for us to continue with the project we are working on :/

If you are unable to release it today, I would appreciate if you could send me an unofficial build so that we can continue our work.

/Morten

 
Kristian Knudsen
Reply
This post has been marked as an answer

Yes it will. It is now ready (8.2.1.5) from the Downloads section.

Sorry for the inconvenience. 

Votes for this answer: 1
 
Morten Bengtson
Reply

Thanks! It looks like create/edit, save/delete  is working now.

What about the template extenders? The item provided is not an intance of the custom item class. Are we supposed to do it like this...

    using System.Collections.Generic;
    using Dynamicweb.Content.Items.Rendering;
    using Dynamicweb.Rendering;

    public class ArticleTemplateExtender : ItemTemplateExtender
    {
        public override void ExtendTemplate(Template template)
        {
            var item = this.Item as Dynamicweb.Content.Items.Item;
            if (item == null || !item.SystemName.Equals("Article"))
            {
                return;
            }

            var article = new Article();            
            var dictionary = new Dictionary<string, object>();
            this.Item.SerializeTo(dictionary);
            article.DeserializeFrom(dictionary);

            // Set tags here
        }
    }

This would be preferred...

    using System.Collections.Generic;
    using Dynamicweb.Content.Items.Rendering;
    using Dynamicweb.Rendering;

    public class ArticleTemplateExtender : ItemTemplateExtender
    {
        public override void ExtendTemplate(Template template)
        {
            var item = this.Item as Article;
            if (item == null)
            {
                return;
            }

            // Set tags
        }
    }

...or even better...

using System.Collections.Generic;
using Dynamicweb.Content.Items.Rendering;
using Dynamicweb.Rendering;
 
public class ArticleTemplateExtender : ItemTemplateExtender<Article>
{
    public override void ExtendTemplate(Template template)
    {
        // Set tags
    }
}

 

BR

Morten

 

 
Pavel Volgarev
Reply

Hi Morten,

 

I think we will go for the generic-based approach (if you use loosely-typed base class, you get Item, and in case of strongly-typed you'll get an instance of your custom item type). Changes are minor, will try to incorporate them today (or tomorrow). 

 

-- Pavel

 
Morten Bengtson
Reply

Hi Pavel,

That sounds like a good solution. I'm starting to love working with items - there is no end to the possibilities :)

 

You must be logged in to post in the forum