Umbraco : Custom Data Handlers

132 views
Skip to first unread message

Andres Castro

unread,
Oct 31, 2013, 9:17:51 AM10/31/13
to glass...@googlegroups.com
I'm having trouble creating a custom data handler using Umbraco Glass Mapper. I'm trying to go off the sitecore tutorial http://glass.lu/docs/tutorial/sitecore/tutorial19/tutorial19.html. I'm trying to convert a MNTP into an object that contains a list of objects that will be created form the MNTP. Right now the Model field is just returning null.

My object model is

    public class RotatingBannerTest
    {
        public IEnumerable<RotatingBannerItem> RotatingBannerList { getset; }
 
        public RotatingBannerTest(IEnumerable<RotatingBannerItem> rotatingBannerItemList)
        {
            this.RotatingBannerList = rotatingBannerItemList;
        }
    }

My Data Handler is

    public class RotatingBannerDataHandler : Glass.Mapper.Umb.DataMappers.AbstractUmbracoPropertyMapper
    {
        public RotatingBannerDataHandler()
            : base(typeof(RotatingBannerTest))
        {
 
        }
 
        public override object GetPropertyValue(object propertyValue, Glass.Mapper.Umb.Configuration.UmbracoPropertyConfiguration config, Glass.Mapper.Umb.UmbracoDataMappingContext context)
        {
            // Assuming it returns back xml string
            if (string.IsNullOrWhiteSpace(propertyValue.ToString()))
            {
                return null;
            }
            try
            {
                RotatingBannerTest rotatingBanner = null;
                DynamicXml dynamicRotatingBannerItems = new DynamicXml(propertyValue.ToString());
                List<RotatingBannerItem> items = GetDynamicItemList<RotatingBannerItem>(dynamicRotatingBannerItems, context);
                rotatingBanner = new RotatingBannerTest(items);
                return rotatingBanner;
            }
            catch(Exception ex)
            {
                return null;
            }
        }
 
 
        public List<Type> GetDynamicItemList<Type>(DynamicXml dynamicXmlItems, Glass.Mapper.Umb.UmbracoDataMappingContext context)
        {
            List<Type> dynamicItemList = new List<Type>();
            if (dynamicXmlItems.BaseElement != null)
            {
                foreach (dynamic item in dynamicXmlItems)
                {
                    dynamicItemList.Add(context.Service.GetItem<Type>(int.Parse(item.InnerText)));
                }
            }
 
            return dynamicItemList;
        }
 
        public override object SetPropertyValue(object value, Glass.Mapper.Umb.Configuration.UmbracoPropertyConfiguration config, Glass.Mapper.Umb.UmbracoDataMappingContext context)
        {
            throw new NotImplementedException();
        }
    }

The model using the object model is

       public class Home : SitePage
	{
		[UmbracoProperty]
		public virtual string Content { getset; }
 
                [UmbracoProperty]
                public virtual RotatingBannerTest RotatingBannerTest { getset; }
	}

The GlassMapperUmbCustomer where I register my Data handler is
                      container.Register(
				Component.For<IContentService>().ImplementedBy<ContentService>().LifestyleTransient(),
                Component.For<IUmbracoService>().ImplementedBy<UmbracoService>().LifestyleTransient(), 
                Component.For<Glass.Mapper.AbstractDataMapper>().ImplementedBy<RotatingBannerDataHandler>().LifeStyle.Transient
			);

I put break points inside my data handler but the only thing that ever gets hit is the constructor. Does anyone have any idea what I might be doing wrong or at least have a suggestion in how I can debug this?
Thanks in advance!

Michael Edwards

unread,
Oct 31, 2013, 9:57:12 AM10/31/13
to Andres Castro, glass...@googlegroups.com
Can you update you model to have the umbraco attribute:

--
You received this message because you are subscribed to the Google Groups "Glass.Mapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glassmapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Michael Edwards

unread,
Oct 31, 2013, 9:59:02 AM10/31/13
to Andres Castro, glass...@googlegroups.com
Sorry I sent it before I completed the message.

Can you update you model to have the umbraco attribute

[UmbracoType] 
public class Home : SitePage
 {
[UmbracoProperty]
public virtual string Content getset; }
 
   [UmbracoProperty] 
public virtual RotatingBannerTest RotatingBannerTest { getset; }
 
}

Cheers

Mike

Andres Castro

unread,
Nov 4, 2013, 8:51:10 AM11/4/13
to glass...@googlegroups.com, Andres Castro, mikeed...@googlemail.com
Thanks for the suggestion Mike. Unfortunately I didn't seem to help. The DataHandler GetPropertyValue method never seems to get hit. On application start up the Data handler constructor does appear to get hit over and over though. I'm not sure if that is what it is suppose to do. Do you have any more suggestions for me?

Thanks,
Andres

Euan Rae

unread,
Sep 30, 2014, 9:00:41 AM9/30/14
to glass...@googlegroups.com, aca...@gmail.com, mikeed...@googlemail.com
Hi guys,

I'm experiencing a similar issue, was there ever a resolution to this?

Cheers,
Euan

Michael Edwards

unread,
Sep 30, 2014, 3:13:45 PM9/30/14
to glass...@googlegroups.com, aca...@gmail.com, mikeed...@googlemail.com

Euan

Can you send me the model with the property you are trying to map and also how you are registering it with the container?

Cheers

Mike
Reply all
Reply to author
Forward
0 new messages