Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Marco Lusini 176 posts 1370 karma points
    Aug 03, 2017 @ 10:49
    Marco Lusini
    1

    Package Action not working during package install

    I am working on a Starter Kit which adds some propertes and tabs to the standard Image Media Type.

    I have written a Package action to create the new properties and also a set of Crops for the Image Cropper.

    My code works fine when I call the Action via Package Action tester, but it doesn't do anything when the Action is calloed during the installation of my Starter Kit...

    I am sure that the code is executed, and it doesn't log any error, it simply doesn't work...

    Below is the class I have created:

    public static class Extensions
    {
        public static PropertyGroup AddnGetPropertyGroup(this IMediaType imageType, string Group)
        {
            imageType.AddPropertyGroup(Group);
    
            return imageType.PropertyGroups[Group];
        }
        public static void AddPropertyType(this PropertyTypeCollection properties, IDataTypeDefinition DataType, string Alias, string Name)
        {
            if (!properties.Contains(Alias))
            {
                PropertyType prop = new PropertyType(DataType);
                prop.Alias = Alias;
                prop.Name = Name;
                //prop.Description = "";
                properties.Add(prop);
            }
        }
    }
    
    public class ImportMedia : IPackageAction
    {
        public bool Execute(string packageName, XmlNode xmlData)
        {
            IMediaService mediaService = ApplicationContext.Current.Services.MediaService;
            IDataTypeService dataTypeService = ApplicationContext.Current.Services.DataTypeService;
            IContentTypeService contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
    
            try
            {
                string cropsJson =
    @"[
    {
      ""alias"": ""newsImage"",
      ""width"": 640,
      ""height"": 480
    },
    {
      ""alias"": ""gridImage"",
      ""width"": 330,
      ""height"": 330
    },
    {
      ""alias"": ""focusImage"",
      ""width"": 400,
      ""height"": 220
    },
    {
      ""alias"": ""masonryImage"",
      ""width"": 400,
      ""height"": 220
    },
    {
      ""alias"": ""carouselImage"",
      ""width"": 370,
      ""height"": 205
    },
    {
      ""alias"": ""galleryImage"",
      ""width"": 370,
      ""height"": 205
    },
    {
      ""alias"": ""sectionImage"",
      ""width"": 420,
      ""height"": 315
    },
    {
      ""alias"": ""bannerImage"",
      ""width"": 240,
      ""height"": 135
    }
    ]
    ";
                IDataTypeDefinition imageCropper = dataTypeService.GetDataTypeDefinitionByName("Image Cropper");
                IDictionary<string, PreValue> crops = new Dictionary<string, PreValue>();
                crops.Add("crops", new PreValue(cropsJson));
    
                if (imageCropper != null)
                {
                    dataTypeService.SaveDataTypeAndPreValues(imageCropper, crops);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error<ImportMedia>("Error during Cropper Image configuration in Govit Starter Kit", ex);
            }
    
            try
            {
                PropertyGroup langTab;
                IDataTypeDefinition GovitTextbox = dataTypeService.GetDataTypeDefinitionByName("Govit - Textbox");
                IDataTypeDefinition GovitTextarea = dataTypeService.GetDataTypeDefinitionByName("Govit - Textarea");
                IMediaType imageType = contentTypeService.GetMediaType(Constants.Conventions.MediaTypes.Image);
    
                langTab = imageType.AddnGetPropertyGroup("Italiano");
                if (langTab != null)
                {
                    PropertyTypeCollection properties = langTab.PropertyTypes;
                    properties.AddPropertyType(GovitTextbox, "govitImgTitleIT", "Titolo");
                    properties.AddPropertyType(GovitTextbox, "govitImgAltIT", "Alt");
                    properties.AddPropertyType(GovitTextarea, "govitImgDescriptionIT", "Descrizione");
                }
    
                langTab = imageType.AddnGetPropertyGroup("English");
                if (langTab != null)
                {
                    PropertyTypeCollection properties = langTab.PropertyTypes;
                    properties.AddPropertyType(GovitTextbox, "govitImgTitleEN", "Title");
                    properties.AddPropertyType(GovitTextbox, "govitImgAltEN", "Alt");
                    properties.AddPropertyType(GovitTextarea, "govitImgDescriptionEN", "Description");
                }
                contentTypeService.Save(imageType);
            }
            catch (Exception ex)
            {
                LogHelper.Error<ImportMedia>("Error during Image Media Type configuration in Govit Starter Kit", ex);
            }
    
            return true;
        }
    
        public string Alias()
        {
            return "govitImportMedia";
        }
    
        public bool Undo(string packageName, XmlNode xmlData)
        {
            return false;
        }
    
        public XmlNode SampleXml()
        {
            return umbraco.cms.businesslogic.packager.standardPackageActions.helper.parseStringToXmlNode("<Action runat=\"install\" alias=\"govitImportMedia\"></Action>");
        }
    }
    

    Any hint? I'm using Umbraco 7.6.4.

Please Sign in or register to post replies

Write your reply to:

Draft