You could publish them as a binary variant using one of the AddBinary() methods of the RenderedItem
class.
I just knocked a couple of test TBBs:
One to create a test JSON file in the package with filename and extension attributes, the other to retrieve such items and publish them out.
They work great in Content Manager Preview but I've done limited testing, so use with caution and test well...
I'm not setting a related component for the binary variant (I'm specifying null
for the relatedComponent
parameter in the AddBinary methods) but depending on your use case you might want to - Setting null
as the relatedComponent
means that your binary can never be cleaned up by the deployer (e.g. by unpublishing your component/page).
See https://gist.github.com/DavidForster/6455674 for the full set, but here's the main TBB
using System;
using Tridion.ContentManager;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;
namespace Test.JsonTemplates
{
[TcmTemplateTitle("Publish JSON package items as Variants")]
public class PublishJsonFiles : ITemplate
{
public void Transform(Engine engine, Package package)
{
//Get all items of unknown type from the package
var items = package.GetAllByType(ContentType.Unknown);
//Remove all items that are NOT JSON files
foreach (var item in items)
{
if (!item.Properties[Item.ItemPropertyFileNameExtension].Equals("json", StringComparison.OrdinalIgnoreCase))
{
items.Remove(item);
}
}
//Check the package values for an optional structure group ID to publish to
var optionalStructureGroupId = package.GetValue("jsonStructureGroup");
StructureGroup jsonStructureGroup = null;
//Go get the structure group from Tridion if an Id has been specified
if (!String.IsNullOrWhiteSpace(optionalStructureGroupId))
{
var structureGroupTcmUri = new TcmUri(
Convert.ToInt32(optionalStructureGroupId),
ItemType.StructureGroup,
engine.PublishingContext.RenderedItem.ResolvedItem.Item.Id.PublicationId);
jsonStructureGroup = (StructureGroup)engine.GetObject(structureGroupTcmUri);
}
//Publish out the JSON files
foreach (var item in items)
{
var filename = item.Properties[Item.ItemPropertyFileName] + "." + item.Properties[Item.ItemPropertyFileNameExtension];
//If no structure group has been specified, publish to the default images directory configured for the publication
if (jsonStructureGroup == null)
{
engine.PublishingContext.RenderedItem.AddBinary(
item.GetAsStream(),
filename,
filename,
null,
"application/json");
}
else
{
engine.PublishingContext.RenderedItem.AddBinary(
item.GetAsStream(),
filename,
jsonStructureGroup,
filename,
null,
"application/json");
}
}
}
}
}