icon Azure Publisher - Single
Created by Orckestra

Plugin API Quick Guide

Azure Publisher comes with a plugin API that allows you to hook into the publish flow and to modify configuration files, allowing for more advanced publish features.

To hook into the publish flow, use BeforePublishEvent and AfterPublishEvent respectively.

To modify configuration files, instantiate and use ContentFilter (delegate) to define specific changes in a specific file as well as use RegisterContentFilter and UnregisterContentFilter to register and unregister the content filter.

The code below illustrates the use of the plugin API. It writes custom messages in the log before and after the publish operation and changes the color from black to pink in CSS and LESS files on the website.

using System;
using System.Collections.Generic;
using System.Web;
using Composite.Core;
using Composite.Core.Application;
using Composite.Azure.Publisher;
using Composite.Azure.Publisher.Extensions;
using System.Text;
using System.IO;

namespace SampleAzurePublisherPlugin
{
    [ApplicationStartup]
    public class MyAppStartup
    {
        public static void OnBeforeInitialize()
        {
            Log.LogInformation("MyAppStartup", "About to get initialized");
        }

        public static void OnInitialized()
        {
            Log.LogInformation("MyAppStartup", "Already initialized");

            PublishEvents.BeforePublishEvent += PluginFacade_BeforePublishEvent;
            PublishEvents.AfterPublishEvent += PluginFacade_AfterPublishEvent;

            ContentFilter filter = (stream, path) =>
            {
                if (path.EndsWith(".css") || path.EndsWith(".less"))
                {
                    using (stream)
                    {
                        // make all black colors pink in css / less files
                        Log.LogInformation("PUBLISHER", path);

                        StreamReader reader = new StreamReader(stream);
                        string text = reader.ReadToEnd();
                        text = text.Replace("#000000", "pink");
                        text = text.Replace("#000", "pink");
                        text = text.Replace("black", "pink");

                        MemoryStream outStream = new MemoryStream();
                        StreamWriter writer = new StreamWriter(outStream);
                        writer.Write(text);
                        writer.Flush();
                        outStream.Position = 0;
                        return outStream;
                    }
                }
                else
                {
                    //Do no transformations
                    Log.LogVerbose("PUBLISHER", path);
                    return stream;
                }
            };

            ContentFiltering.RegisterContentFilter(filter);
        }

        static void PluginFacade_AfterPublishEvent(PublishEventArgs e)
        {
            if (e.StagingPublish)
            {
                //The publish to staging was clicking in advanced menu
            }
            Log.LogCritical("PUBLISHER PLUGIN", "PUBLISH STARTED");
        }

        static void PluginFacade_BeforePublishEvent(PublishEventArgs e)
        {
            Log.LogCritical("PUBLISHER PLUGIN", "PUBLISH FINISHED");
        }
    }
}


Download the sample code

How to build a project with the sample code

  1. In Visual Studio, create a class library project.
  2. Add references to Composite.dll and Composite.Azure.Publisher.dll in /Bin on your site. Note: You have to install Composite.Azure.Publisher v 3.0.0 or later.
  3. Add a class to the project and use the code in the sample above
  4. Build the project.

How to use the sample plugin on your site

  1. Copy SampleAzurePublisherPlugin.dll from your project to /Bin on your website.
  2. Log in to the C1 Console.
  3. From the Content perspective, configure Azure Publisher.
  4. Click "Open Publisher".
  5. Click "Republish Everything".

How to check for changes made by the plugin on the Azure

  1. Log in to the website running on Windows Azure. Note: The website should allow C1 Console logins.
  2. Check the server log for the log messages written by the plugin.
  3. Check the CSS and LESS files in /Frontend/Styles/ on the site for style changes by the plugin.
Back to top