Developer forum

Forum » Development » Cannot create a new page based on my .NET based item

Cannot create a new page based on my .NET based item

Sten Hougaard
Reply

Hi,

I have been trying to create items through .net code. I have successfully builded a DLL and have uploaded it.

I have seen in management center the item I have created.

I have changed it through backend to be possible to use it (where and how to use through settings).

However when I try to make a new page based on the item, the soloution crash.

I have created a Github public repository where you can see my soloution:

https://github.com/netsi1964/dynamicweb


Med venlig hilsen/Best regards,

Sten Hougaard
Webudvikler

E: sho@1stweb.dk
M: 29850818
A: København/Aarhus . W: www.1stweb.dk
@: netsi1964


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Look at your Born property. Doesn't cause an infinite loop and ultimately a StackOverflowException?

 

Imar

 
Sten Hougaard
Reply

Hi Imar,

I have tried to comment out the "born" field but it gives the same result.

I have tried to remove both fields which used not-simple getters and setters, then I did not get a crash.

I guess that somehow Dynamicweb do not like my "advanced" setters like these two:

        [Group ("Content")]
        [Field ("Content", typeof(Dynamicweb.Content.Items.Editors.DateTimeEditor))]
        [Name ("Born")]
        public DateTime Born { 
            get { return this.Born; }
            set { 
                DateTime oldest = DateTime.Now.AddYears (-120);
                this.Born = (this.Born < oldest) ? this.Born : oldest;
            }
        }

        // Here the set will only allow for 155 chars
        [Group ("SEO")]
        public string MetaDescription { 
            get { return this.MetaDescription; } 
            set { this.Heading = value.Substring (0, 155); } 
        }

/Sten

 
Morten Bengtson
Reply

Sten, it is not a Dynamicweb issue. Thats just how properties work :) 

You need to use a field as backing store for your "advanced" properties in order to avoid the recursive calls.

http://msdn.microsoft.com/en-us/library/w86s7x04.aspx

 
Sten Hougaard
Reply

Thank you Morten,

I should create a private variable and use the public to update the private value.

Will the value then be stored as part of the item? I am new to this and that (.net and .net based items), so if you have some valuable experience on this, I would appriciate if you shared it :-)


Med venlig hilsen/Best regards,

Sten Hougaard
Webudvikler

E: sho@1stweb.dk
M: 29850818
A: København/Aarhus . W: www.1stweb.dk
@: netsi1964

 
Morten Bengtson
Reply

It's been a long time since I last created code-first items, but I'm pretty sure that it will work.

Otherwise, you can override the Save() method...

[Group("SEO")]
public string MetaDescription { get; set; }

public override void Save()
{
    this.Heading = this.MetaDescription.Substring(0, 155);
    base.Save();
}
 
Sten Hougaard
Reply

Hi Morten,

I would agree that it seems like a (better) solution, however it does not work :-)

Seems like the Save method is not called.


Med venlig hilsen/Best regards,

Sten Hougaard
Webudvikler

E: sho@1stweb.dk
M: 29850818
A: København/Aarhus . W: www.1stweb.dk
@: netsi1964

 
Morten Bengtson
Reply

Yes, apparently things have changed since my last battle with code-first items :)

Now you need to override the other Save method...

public override void Save(ItemContext context)
{
    this.Heading = this.MetaDescription.Substring(0, 155);
    base.Save(context);
}

 

You must be logged in to post in the forum