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
By comparing with the DAMP source code (DAMP) I noticed that the new MediaType class Umbraco.Core.Models.MediaType is missing the Save() method, so no matter how I tested, it simply would not save the new media type.
Anyway, also by using DAMP's example (good old copy/paste), that uses the old assemblies and shows a message 'OBSOLETE', came up with a new version of the code, which worked as expected.
So, this is possibly something that I'll have to revisit in the near future.
What the new code looks like:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Xml;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.datatype.controls;
using umbraco.cms.businesslogic.media;
using umbraco.cms.businesslogic.packager.standardPackageActions;
using umbraco.DataLayer;
using umbraco.interfaces;
using umbraco.IO;
namespace SocialMediaChannels
{
public class AddSocialMediaThemes : IPackageAction
{
private readonly static string MediaTypeName = "Social Media Theme";
private readonly static int LABEL_ID = -92;
private readonly static int UPLOAD_ID = -90;
private readonly static int TEXT_ID = -88;
private readonly static int DATE_ID = -41;
private readonly static int IMAGE_ID = 1031;
private readonly static int FOLDER_ID = 1032;
public IDataType uploadField = new Factory().GetNewObject(new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c"));
protected static ISqlHelper SqlHelper
{
get
{
return umbraco.BusinessLogic.Application.SqlHelper;
}
}
public string Alias()
{
return "SocialMediaChannels_AddSocialMediaThemes";
}
public bool Execute(string packageName, XmlNode xmlData)
{
bool flag;
try
{
#region MediaType
User adminUser = new User(0);
MediaType theme = CreateMediaType(adminUser, MediaTypeName);
MediaType folder = MediaType.GetByAlias("Folder");
int[] folderStructure = folder.AllowedChildContentTypeIDs;
int newsize = folderStructure.Length + 1;
Array.Resize(ref folderStructure, newsize);
folderStructure[newsize - 1] = theme.Id;
folder.AllowedChildContentTypeIDs = folderStructure;
MediaType image = MediaType.GetByAlias(MediaTypeName);
#endregion
#region Theme Images
#endregion
flag = true;
}
catch
{
flag = false;
}
return flag;
}
public bool Undo(string packageName, XmlNode xmlData)
{
bool flag;
try
{
//remove themes
flag = true;
}
catch
{
flag = false;
}
return flag;
}
public XmlNode SampleXml()
{
string sample = string.Format("<Action runat=\"install\" undo=\"true/false\" alias=\"{0}\" ></Action>", Alias());
return helper.parseStringToXmlNode(sample);
}
/// <summary>
/// Create a Media Type.
/// </summary>
/// <param name="adminUser"></param>
/// <param name="mediaTypeName"></param>
/// <returns></returns>
private MediaType CreateMediaType(User adminUser, string mediaTypeName)
{
MediaType mediaType = MediaType.MakeNew(adminUser, mediaTypeName);
int[] typeIds = { FOLDER_ID, IMAGE_ID };
mediaType.AllowAtRoot = true;
mediaType.Text = MediaTypeName;
mediaType.Description = "Container for the Social Media Channel Theme Images";
mediaType.IsContainerContentType = true;
mediaType.AllowedChildContentTypeIDs = typeIds;
//Add properties
mediaType.AddPropertyType(new DataTypeDefinition(UPLOAD_ID), "umbracoFile", "Upload image");
mediaType.AddPropertyType(new DataTypeDefinition(LABEL_ID), "umbracoWidth", "Width");
mediaType.AddPropertyType(new DataTypeDefinition(LABEL_ID), "umbracoHeight", "Height");
mediaType.AddPropertyType(new DataTypeDefinition(LABEL_ID), "umbracoBytes", "Size");
mediaType.AddPropertyType(new DataTypeDefinition(LABEL_ID), "umbracoExtension", "Type");
mediaType.AddPropertyType(new DataTypeDefinition(TEXT_ID), "themeName", "Name of the Social Channel Theme");
mediaType.AddPropertyType(new DataTypeDefinition(TEXT_ID), "themeUrl", "Url for the Theme");
mediaType.AddPropertyType(new DataTypeDefinition(TEXT_ID), "createdBy", "Author of the Theme");
mediaType.AddPropertyType(new DataTypeDefinition(DATE_ID), "createdDate", "Date the Theme was created");
mediaType.Text = mediaTypeName;
mediaType.IconUrl = "mediaPhoto.gif";
mediaType.Save();
return mediaType;
}
}
}
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:
Below is the Helper Class:
And in Package Actions (Umbraco) I have added the following line
Suggestions or corrections? Anyone?
By comparing with the DAMP source code (DAMP) I noticed that the new MediaType class Umbraco.Core.Models.MediaType is missing the
Save()
method, so no matter how I tested, it simply would not save the new media type.Anyway, also by using DAMP's example (good old copy/paste), that uses the old assemblies and shows a message 'OBSOLETE', came up with a new version of the code, which worked as expected.
So, this is possibly something that I'll have to revisit in the near future.
What the new code looks like: using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Xml; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.datatype; using umbraco.cms.businesslogic.datatype.controls; using umbraco.cms.businesslogic.media; using umbraco.cms.businesslogic.packager.standardPackageActions; using umbraco.DataLayer; using umbraco.interfaces; using umbraco.IO;
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:
And now it works!
is working on a reply...