It is possible to populate feature instance content when a site is created. As described in Configuring Initial Content, you can define which features are added to each page created with a new site. There are content installers already implemented for many of the features included in mojoPortal, and you can review the source code of those installers to learn how to implement a content installer for your own custom features. 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"
/>

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);

        }
    }
}

The else condition is for backward compatibility, where the content can come from text file under /Data/MessageTemplates and can be localized. The content can also 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 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 Modified by Joe Davis on Jul 29, 2022