Search code examples
c#umbraco7umbraco8

Problems duplicating code from Umbraco v7 to Umbraco v8


I am attempting to duplicate some code from a site built on Umbraco v7 into a site built on Umbraco v8, but some of the classes don't seem to exist in the new version. Here is the class i'm trying to copy:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;

namespace XXXX.UmbracoStarterKit.CMS.Models
{
    // Mixin content Type 1245 with alias "baseImage"
    /// <summary>Base Image</summary>
    public partial interface IBaseImage : IPublishedContent
    {
        /// <summary>Alternative Text</summary>
        string ImageAlt { get; }

        /// <summary>Bytes</summary>
        string UmbracoBytes { get; }

        /// <summary>Extension</summary>
        string UmbracoExtension { get; }

        /// <summary>Height</summary>
        string UmbracoHeight { get; }

        /// <summary>Width</summary>
        string UmbracoWidth { get; }
    }

    /// <summary>Base Image</summary>
    [PublishedContentModel("baseImage")]
    public partial class BaseImage : PublishedContentModel, IBaseImage
    {
#pragma warning disable 0109 // new is redundant
        public new const string ModelTypeAlias = "baseImage";
        public new const PublishedItemType ModelItemType = PublishedItemType.Media;
#pragma warning restore 0109

        public BaseImage(IPublishedContent content)
            : base(content)
        { }

#pragma warning disable 0109 // new is redundant
        public new static PublishedContentType GetModelContentType()
        {
            return PublishedContentType.Get(ModelItemType, ModelTypeAlias);
        }
#pragma warning restore 0109

        public static PublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<BaseImage, TValue>> selector)
        {
            return PublishedContentModelUtility.GetModelPropertyType(GetModelContentType(), selector);
        }

        ///<summary>
        /// Alternative Text: The text used if  the image cannot be displayed. This can be useful for SEO and accessibility.
        ///</summary>
        [ImplementPropertyType("imageAlt")]
        public string ImageAlt
        {
            get { return GetImageAlt(this); }
        }

        /// <summary>Static getter for Alternative Text</summary>
        public static string GetImageAlt(IBaseImage that) { return that.GetPropertyValue<string>("imageAlt"); }

        ///<summary>
        /// Bytes: The size of the image in bytes.
        ///</summary>
        [ImplementPropertyType("umbracoBytes")]
        public string UmbracoBytes
        {
            get { return GetUmbracoBytes(this); }
        }

        /// <summary>Static getter for Bytes</summary>
        public static string GetUmbracoBytes(IBaseImage that) { return that.GetPropertyValue<string>("umbracoBytes"); }

        ///<summary>
        /// Extension: The image extension.
        ///</summary>
        [ImplementPropertyType("umbracoExtension")]
        public string UmbracoExtension
        {
            get { return GetUmbracoExtension(this); }
        }

        /// <summary>Static getter for Extension</summary>
        public static string GetUmbracoExtension(IBaseImage that) { return that.GetPropertyValue<string>("umbracoExtension"); }

        ///<summary>
        /// Height: The height of the image.
        ///</summary>
        [ImplementPropertyType("umbracoHeight")]
        public string UmbracoHeight
        {
            get { return GetUmbracoHeight(this); }
        }

        /// <summary>Static getter for Height</summary>
        public static string GetUmbracoHeight(IBaseImage that) { return that.GetPropertyValue<string>("umbracoHeight"); }

        ///<summary>
        /// Width: The width of the image.
        ///</summary>
        [ImplementPropertyType("umbracoWidth")]
        public string UmbracoWidth
        {
            get { return GetUmbracoWidth(this); }
        }

        /// <summary>Static getter for Width</summary>
        public static string GetUmbracoWidth(IBaseImage that) { return that.GetPropertyValue<string>("umbracoWidth"); }
    }
}

Above public partial class BaseImage there is an attribute (PublishedContentModel("baseImage")), but this doesn't seem to exist in v8. In v7 when I view the definition I see the PublishedContentModelAttribute, which is part of the Umbraco.Core.Models.PublishedContent namespace. Is there a replacement for this in v8?

In the GetModelContentType method there is a call to PublishedContentType.Get, but the Get method is also missing from the Umbraco.Core.Models.PublishedContent namespace in v8. Any suggestions what I might use instead?

In the GetModelPropertyType method there is a call to PublishedContentModelUtility.GetModelPropertyType, but PublishedContentModelUtility doesn't appear to exist in Umbraco.ModelBuilder.Umbraco any more. Is there an alternative in v8?

Further on, there are several calls to that.GetPropertyValue, which should be in the Umbraco.Web.PublishedContentExtenions namespace, but can't be found in v8. Can anyone suggest what code I can use instead?

I realise that there are several questions here, but they are all part of the same issue, and all preventing me from completing my task.


Solution

  • Have a look at https://our.umbraco.com/forum/umbraco-8/98400-errors-thrown-after-upgrade-to-811

    A LOT has changed from v7 to v8, including some of the ModelsBuilder stuff. Looks like some of it is now using interfaces. Maybe let Umbraco 8 ModelsBuilder create a brand new model class for you and then see if you can spot the changes that way?