uSiteBuilder 3.0 loads DocumentType fields but not the DynamicNode fields

249 Views Asked by At

I've encountered a problem working with Umbraco 6.1.5 and uSiteBuilder 3.0.0 where when I instantiate a strongly typed DocumentType using ContentHelper all the fields defined in the DocumentType are loaded into the object but fields like Name, Id or Children aren't loaded (they're null, empty or 0).

From what I can tell it's because the ContentHelper method responsible for instantiation is calling the empty constructor for DynamicNode. Is there something I'm missing? Should I be defining constructors on my Document Types?

Here's my DocumentType:

namespace USiteBuilderTest.DocumentTypes
{
    [DocumentType]
    public class Home : DocumentTypeBase
    {
        [DocumentTypeProperty(Name = "Field 1")]
        public string Field1 { set; get; }

        [DocumentTypeProperty(Name = "Field 2")]
        public string Field2 { set; get; }

        [DocumentTypeProperty(Name = "Field 3")]
        public string Field3 { set; get; }
    }
}

In-case it's helpful, here's the part of the code that's calling the empty constructor:

Type typeDocType = DocumentTypeManager.GetDocumentTypeType(node.NodeTypeAlias);
    if (typeDocType != null)
    {
        ConstructorInfo constructorInfo = typeDocType.GetConstructor(new[] { typeof(int) });
        if (constructorInfo == null)
        {
            retVal = (DocumentTypeBase)Activator.CreateInstance(typeDocType);
        }
        else
        {
            retVal = (DocumentTypeBase)constructorInfo.Invoke(new object[] { node.Id });
        }
2

There are 2 best solutions below

0
On

I also experienced this problem. A solution could be to add an empty constructor and a constructor that has nodeid as input. Like this:

namespace USiteBuilderTest.DocumentTypes
{
    [DocumentType]
    public class Home : DocumentTypeBase
    {
        public Home() {}

        public Home(int nodeId) : base(nodeId) { }

        [DocumentTypeProperty(Name = "Field 1")]
        public string Field1 { set; get; }

        [DocumentTypeProperty(Name = "Field 2")]
        public string Field2 { set; get; }

        [DocumentTypeProperty(Name = "Field 3")]
        public string Field3 { set; get; }
    }
}

That did the trick for me.

0
On

It seems that despite the Codeplex project description clearly stating that branch "3.0.0" should be used with Umbraco v6 there's actually a specific v6 branch that should be used instead (labelled "version6API").

Credit for the answer goes to Vladan Ostojic who answered the question on the Umbraco forums:

http://our.umbraco.org/projects/developer-tools/usitebuilder/usitebuilder-support/44774-uSiteBuilder-30-loads-DocumentType-fields-but-not-the-DynamicNode-fields?p=0#comment161011