Monday, June 25, 2012

How to insert initial data on my Prj - IFeatureEventHandler

When you work on a web site, most of the time, you need a default initialization data to fill it with something that make sense.

If you need to do that task with Orchard CMS 1.4.2 you need to implement the IFeatureEventHandler interface:

using Orchard.Environment.Extensions.Models;
using Orchard.Events;

namespace Orchard.Environment {
public interface IOrchardShellEvents : IEventHandler {
void Activated();
void Terminating();

public interface IFeatureEventHandler : IEventHandler {
void Installing(Feature feature);
void Installed(Feature feature);
void Enabling(Feature feature);
void Enabled(Feature feature);
void Disabling(Feature feature);
void Disabled(Feature feature);
void Uninstalling(Feature feature);
void Uninstalled(Feature feature);

I wrote the following in my migration class:

public void Enabled(Feature feature)
if (feature.Descriptor.Name != "My ContentTypes")

string path = HttpContext.Current.Server.MapPath(@"~\Modules\Orchard.My.Common\Content\setup\portalsections.xml");

var doc = XDocument.Load(path);

// Query the data and write out a subset of contacts
var q = from c in doc.Descendants("Section")
orderby Convert.ToInt32(c.Attribute("Order").Value)
select new
ClassNamePrefix = c.Element("ClassNamePrefix").Value,
ColorCode = c.Element("ColorCode").Value,
SubTitle = c.Element("SubTitle").Value,
Title = c.Element("Title").Value

foreach (var section in q)
var section = _services.ContentManager.Create<PortalSectionPart>("PortalSection", VersionOptions.Published);

section.ContentItem.As<TitlePart>().Title = section.Title;

section.ClassNamePrefix = section.ClassNamePrefix;
section.ColorCode = section.ColorCode;
section.SubTitle = section.SubTitle;
catch (Exception)
_services.Notifier.Add(NotifyType.Error, T("Error on SkillMigration's Enabled function"));

Winking smile

No comments:

Post a Comment