To create and use Custom Dreamweaver Functions from a Razor Mediator.
Refer to this below example, you can create one specific to your case.
- Create a custom class in C# using the TOM.NET API based on ITemplate and IFunctionSource
[TemplateCallable]
public String PublishBinary(String compURI)
{
Component comp = mEngine.GetObject(compURI) as Component;
String publishPath = String.Empty;
if (comp != null && comp.BinaryContent != null)
{
Binary pubBinary = mEngine.PublishingContext.RenderedItem.AddBinary(comp);
publishPath = pubBinary.Url;
if (pubBinary != null)
publishPath = pubBinary.Url;
}
return publishPath;
}
Create the Strong-name and add it to the GAC using the gacutil.exe.
Edit your environment Tridion.ContentManager.config and find the element and inside it, the element. Add your class dll reference.
<tridion.templating>
<functionSources>
<functionSource type="YourCompany.Tridion.Templating.YourCustomClassExtensions" assembly="MyCustomClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ddfc8957d6e5ee6b" />
</functionSources>
</tridion.templating>
Restart SDL Tridion services and COM+
In your TBB Razor template, you can call your template callable function as below
@{
var dw = new YourCompany.Tridion.Templating.YourCustomClassExtensions();
dw.Initialize(TridionHelper.Engine, TridionHelper.Package);
}
@dw.PublishBinary(cp.Component.Fields.Media.Id)
I hope it helps.