Search code examples
c#packageactionumbraco

Create new MediaType via C# (For PackageAction)


I have created a package that requires the creation of an extra MediaType, since packaging it is not available in the package screens.

To circumvent this issue I've started researching into creating a new MediaType via Package Actions. Lastly started following this quick tutorial Umbraco V7 Compatible Packages

Have added a class (below), compiled it (in Debug Mode), copied it to the /bin/ folder of Umbraco, then added the dll as a package file, along with everything else.

Then, on another instance of Umbraco, installed it from local package.

Problem is: IT IS NOT WORKING!

At the moment I have no idea of what could be wrong, so if anyone has any suggestions, they're welcome.

Below is the class I have created:

using System;
using System.Collections.Generic;
using System.Xml;
using umbraco.interfaces;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

namespace Social_Media_Channels.Installer
{
    public class AddMediaAction : IPackageAction
    {
        public string Alias()
        {
            return "SocialMediaChannels_AddThemes";
        }

        public bool Execute(string packageName, XmlNode xmlData)
        {
            string STEP = string.Empty;

            if (UmbracoVersion.Current.Major >= 7)
            {
                try
                {
                    #region MediaType
                    STEP = "Adding MediaType";
                    LogHelper.Info(typeof(AddMediaAction), STEP);
                    MediaHelper.AddMediaType();
                    #endregion

                    #region Theme Images
                    STEP = "Adding Media Themes";
                    LogHelper.Info(typeof(AddMediaAction), STEP);

                    #endregion
                    return true;
                }
                catch (Exception ex)
                {
                    var message = string.Concat("Error at install ", Alias(), " package action: " + STEP, ex);
                    LogHelper.Error(typeof(AddMediaAction), message, ex);
                    return false;
                }


            }
            return false;
        }

        public bool Undo(string packageName, XmlNode xmlData)
        {
            if (UmbracoVersion.Current.Major >= 7)
            {
                //MediaType mediaType = new MediaType();
            }
            return true;
        }

        public XmlNode SampleXml()
        {
            var xml = string.Format("<Action runat=\"install\" undo=\"true\" alias=\"{0}\" />", Alias());
            XmlDocument x = new XmlDocument();
            x.LoadXml(xml);
            return x;
        }


    }
}

Below is the Helper Class:

using System.Collections.Generic;
using Umbraco.Core.Models;

namespace Social_Media_Channels.Installer
{
    public class MediaHelper
    {
        private readonly static string MediaTypeName = "Social Media Theme";

        // LogHelper.Error<TranslationHelper>("Failed to add Opening Soon localization values to language file", ex);
        public static void AddMediaType()
        {
            MediaType mediaType = new MediaType(0);
            mediaType.AllowedAsRoot = true;
            mediaType.Name = MediaTypeName;
            mediaType.Description = "Container for the Social Media Channel Theme Images";
            mediaType.IsContainer = true;

            //Allowed child nodes
            var children = new List<ContentTypeSort>
                {
                    new ContentTypeSort(1031, 0),
                    new ContentTypeSort(1032, 1)
                };

            mediaType.AllowedContentTypes = children;

            //Add properties
            var name = new PropertyType(new DataTypeDefinition(-88, "themeName"));
            name.Name = "Theme Name";
            name.Description = "Name for the theme";

            var url = new PropertyType(new DataTypeDefinition(-88, "themeUrl"));
            url.Name = "Theme Url";
            url.Description = "Url for the original theme";

            var createdBy = new PropertyType(new DataTypeDefinition(-88, "createdBy"));
            createdBy.Name = "Created By";
            createdBy.Description = "Theme Author";

            var createdDate = new PropertyType(new DataTypeDefinition(-41, "createdDate"));
            createdDate.Name = "Created Date";
            createdDate.Description = "Date the Theme was created";

            mediaType.AddPropertyType(name, "Image");
            mediaType.AddPropertyType(url, "Image");
            mediaType.AddPropertyType(createdBy, "Image");
            mediaType.AddPropertyType(createdDate, "Image");
        }

        public static void RemoveMediaType()
        {

        }
    }
}

And in Package Actions (Umbraco) I have added the following line

<Action runat="install" undo="true" alias="SocialMediaChannels_AddThemes" />

Suggestions or corrections? Anyone?


Solution

  • Turns out there were a lot of things wrong with my original code. Finally got it working after lots of investigation.

    Basically the Adding (which was causing it to fail) became:

        public static void AddMediaType()
        {
            MediaType mediaType = new MediaType(-1);
            mediaType.AllowedAsRoot = true;
            mediaType.Name = NAME;
            mediaType.Description = "Container for the Social Media Channel Theme Images";
            mediaType.IsContainer = true;
            mediaType.Icon = "icon-picture";
            mediaType.Alias = ALIAS;
    
            //Allowed child nodes
            var children = new List<ContentTypeSort>
                {
                    new ContentTypeSort(FOLDER_ID, 0),
                    new ContentTypeSort(IMAGE_ID, 1)
                };
    
            mediaType.AllowedContentTypes = children;
            DataTypeService dataTypeService = (DataTypeService)ApplicationContext.Current.Services.DataTypeService;
    
    
            //Add properties
            var name = new PropertyType(dataTypeService.GetDataTypeDefinitionById(TEXT_ID), "themeName");
            name.Name = "Theme Name";
            name.Description = "Name for the theme";
            name.SortOrder = 0;
    
            var url = new PropertyType(dataTypeService.GetDataTypeDefinitionById(TEXT_ID), "themeUrl");
            url.Name = "Theme Url";
            url.Description = "Url for the original theme";
            url.SortOrder = 1;
    
            var createdBy = new PropertyType(dataTypeService.GetDataTypeDefinitionById(TEXT_ID), "createdBy");
            createdBy.Name = "Created By";
            createdBy.Description = "Theme Author";
            createdBy.SortOrder = 2;
    
            var createdDate = new PropertyType(dataTypeService.GetDataTypeDefinitionById(DATE_ID), "createdDate");
            createdDate.Name = "Created Date";
            createdDate.Description = "Date the Theme was created";
            createdDate.SortOrder = 3;
    
            mediaType.AddPropertyType(name, "Image");
            mediaType.AddPropertyType(url, "Image");
            mediaType.AddPropertyType(createdBy, "Image");
            mediaType.AddPropertyType(createdDate, "Image");
    
            ContentTypeService contentTypeService = (ContentTypeService)ApplicationContext.Current.Services.ContentTypeService;
            contentTypeService.Save(mediaType);
        }
    

    And now it works!