Publishing Multiple Files from a Single Asset in CSharp (C#)

By Simon Gentner
April 18, 2015

In this example we will use the PublishAgain and the PublishingLoopNumber to generate multiple files from one asset in the system, essentially
your telling the cms to keep publishing until you tell it to stop. There is also a hard coded limit in the publish again to 100 times. But please make sure you stop before as it can cause a strain on the publish queue.

 First before setting up make sure your publishing is correctly setup
     You will need an output.aspx and a filename.aspx as a minimum to publish , before adding the code check your general properties are showing correctly, and actually test publish the file to make sure it publishes


In this example we will use an array but basically the output and filename need access to a common function that reruns specific results that will be consistent in both filename and output so for this example we will use the days of the week

string[] arrDaysofWeek = {"Monday","Tuesday","Wednesday","Thursday","Friday"};

In the filename.aspx we will add the following code

=====================
FILENAME.ASPX
=============
 

string[] arrDaysofWeek = {"Monday","Tuesday","Wednesday","Thursday","Friday"};

    if (context.IsPublishing)
    {
        context.PublishPath = arrDaysofWeek[context.PublishingLoopNumber - 1] + ".html";
    }
    else
    {
        context.PublishPath = arrDaysofWeek[0] + ".html";
    }


=====================
OUTPUT.ASPX
=============

string[] arrDaysofWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

    if (context.IsPublishing)
    {
        if (context.PublishingLoopNumber < arrDaysofWeek.Length)
        { 
            context.PublishAgain = true;
        }
        
        int intPubLoop = context.PublishingLoopNumber - 1 ;

        Out.Write("This is files day of the week is " + context.PublishingLoopNumber.ToString() + Environment.NewLine);
    }
    else
    {
    //For Preview Only
    Out.Write("This is files day is " + arrDaysofWeek[0].ToString());
}

When you publish you can verify the results by looking at the general properties
 

==================
TROUBLESHOOTING
++++++++++++++++++
Any errors in the flename.aspx will show up in the general properties of the file


Or after publishing the file you will see an error in the file view, you can find out more on the error if you click on the link "STAGE ERROR"

 Remember its the output.aspx that controls the publishing loop, the filename just follows the outputs direction. Use the paradigm that the cms is doing the loops for you so you should not have to use a loop to generate the content rather rely on the publishingloop number that the cms generates to implement the loop

Tags:  Blogs, Developers
comments powered by Disqus