11

Is there a function to call to resolve path of a binary?

Example:

This works and resolves the media.ID as a path.

<img src="@Fields.asset.media.ID" tridion:type="Multimedia" alt="test"/>

But what we want is just the path without the image tag.

"media":"@Fields.asset.media.ID"

expected result: /images/plaatje.jpg and not

<img src="/images/plaatje.jpg" />
13

There's currently not a built in helper method with the Razor Mediator that does this, but there's still different things you can do. First you'll have to understand that Tridion doesn't just "resolve" paths on its own. Behind the scenes with Razor and DW Mediators, tags marked like <img src="tcm:x-xxx" /> automatically have the binary pushed to the package. Then, in the Default Finish Actions, there's a TBB called "Publish Binaries in Package" that publishes multimedia item types that it finds that were pushed in the package (either through the Mediator, or from any other TBB prior to the the Publish Binaries TBB that pushes them to the package). It sets a ItemPropertyPublishedPath property on each multimedia item in the package that gets published that contained the path. Then in the Link Resolver TBB, it replaces the TcmUri's that were in the previous markup with the path in the PublishedPath property of the item with the matching TcmUri that had been published.

Knowing this, you now have a couple options. You can create your own helper method that calls either the engine.PublishingContext.RenderedItem.AddBinary(component); method, which returns a Tridion.ContentManager.Publishing.Rendering.Binary instance, and then return the instance's Url property. Or you can call the engine's AddBinary(TcmUri componentUri, TcmUri templateUri, TcmUri location, byte[] byteArray, string filename);. With these functions you also have a finer grain of control over the published binary, like where it gets published to, the variant id, and even changing the filename of the published item. The following is a quick example with bare minimum arguments.

@functions {
    public string PublishAndGetUrl(Tridion.ContentManager.ContentManagement.Component component) {
        if (component.BinaryContent != null) {
            Tridion.ContentManager.Publishing.Rendering.Binary binary = TridionHelper.Engine.PublishingContext.RenderedItem.AddBinary(component);
            return binary.Url;
        }
        return String.Empty;
    }
}

You would then use the helper like:

<text>"media":"@PublishAndGetUrl(Fields.YourImage.TridionObject)"</text>

If you wanted a bit more of a modular approach with TBBs, you could also attempt this with a couple of C# TBBs, one that looks for the pattern and pushes the multimedia item to the package (and placed before the Publish Binaries TBB), and then your own resolver that would look for the pattern and replace the tcm uri with the item's published path (and placed after the Publish Binaries TBB).

Hope that helps!

| improve this answer | |
  • Thanks for your answer here - once I knew that the way it worked was to specifically look for src="" I could do that in the XML fragment I was trying to publish: <small_image src="@Fields.small_image" /> – Ian Grainger Nov 22 '13 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.