Populating Feature Content

It is possible to populate feature instance content also when initial content is created for a new site. As you saw in Configuring Initial Content, you can define what features are added to a page. There are content installers already implemented for many of the features included in mojoPortal content management system, and you can study the source code of those installers to learn how to implement a content installer for your own custom features if you need to populate it when new sites are created. This example shows how we use the Html Content installer to configure some html content on the mojoPortal demo site.

<contentFeature
 featureGuid="881e4e00-93e4-444c-b7b0-6672fb55de10"
 contentTitle="mojoPortal Add On Product Demos"
location="center"
 sortOrder="1"
 cacheTimeInSeconds="0"
 contentInstaller="mojoPortal.Web.HtmlContentInstaller, mojoPortal.Web"
 configInfo="~/Setup/initialcontent/democontent/product-list.config"
/>

Notice that the contentInstaller refers to the fully qualified type and assembly name of the class that is used to populate the content. This can be found in the mojoPortal source code under Web/Components/HtmlContentInstaller.cs. The full code for this is as follows:

using System.Globalization;
using System.IO;
using System.Text;
using System.Web.Hosting;
using mojoPortal.Business;
using mojoPortal.Web.Framework;

namespace mojoPortal.Web
{
    public class HtmlContentInstaller : IContentInstaller
    {

        public void InstallContent(Module module, string configInfo)
        {
            HtmlContent htmlContent = new HtmlContent();
            htmlContent.ModuleId = module.ModuleId;
            if (configInfo.StartsWith("~/"))
            {
                if (File.Exists(HostingEnvironment.MapPath(configInfo)))
                {
                    htmlContent.Body = File.ReadAllText(HostingEnvironment.MapPath(configInfo), Encoding.UTF8);
                }
            }
            else
            {
                htmlContent.Body = ResourceHelper.GetMessageTemplate(CultureInfo.CurrentCulture, configInfo);
            }

            htmlContent.ModuleGuid = module.ModuleGuid;
            HtmlRepository repository = new HtmlRepository();
            repository.Save(htmlContent);

        }


    }
}

Note that the else condition is for backward compatibility where the content can come from text file under /Data/MessageTemplates and can be localized. Or the content can come from anywhere if you start with ~/ to represent the root of the file system. This is a pretty simple implementation. Notice that it implements the IContentInstaller interface which defines one method signature. There are more complex examples of content installer also included in the source code for the Forums, Image Gallery, Folder Gallery, Feed Manager, and even WebStore. These were all designed mainly to help populate our demo site whenever it is reset. These examples can be a good reference for when you want to implement a content installer for your own feature.

Last Update 2011-03-30 by Joe Audette