2

I'm trying to write a block of code to publish images uploaded in multimedia Components.

This block of code publishes only the data (4th argument) and not the image uploaded in the Component. By the way, I don't even understand why the Component (1st argument) is required because it's not used:

// Get the component
Component c = (Component)engine.GetObject(package.GetByName(Package.ComponentName));
// Get the fields
ItemFields fields = new ItemFields(c.Content, c.Schema);
// get our Embedded schema field
EmbeddedSchemaField emb = (EmbeddedSchemaField)fields["Slide"];
// Loop
foreach (ItemFields embeddedfields in emb.Values)
{
    foreach (ItemField field in embeddedfields)
    {
        ComponentLinkField fieldImage = field as ComponentLinkField;

        if (fieldImage !=null)
        {
            Component component = fieldImage .Value;
            string componentId= component.Id;
            package.PushItem("ARN", package.CreateStringItem(ContentType.Text, componentId));
            var sevenItems = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; // WHAT IS ACTUALLY PUBLISHED
            TcmUri uri2 = new TcmUri("tcm:127-3102-4");
            StructureGroup sg = new StructureGroup(engine.GetSession(),uri2);
            string replace = engine.AddBinary(component.Id,null,uri2,sevenItems ,"rrrr");
        }
    }
}
  • What is the correct block of code to publish the image uploaded in the Component (1st argument)?

  • Is it possible to retrieve the URL of the item being published? Here, my "replace" variable contains something like this:

http://content.blabla.com/Preview/c525b64b-10ee-4587-8c30-d83865c5bdaf//rrrr

Thank you for your help

2 Answers 2

0

There are multiple overrides of Engine.AddBinary() to achieve different things, mostly allowing you to publish variants of the multimedia Component, or to change the filename of the item published. So any of the overrides are considered a correct way to publish a binary. If you just want to publish the binary as is, you can use Engine.AddBinary(Item multimediaItem, TcmUri targetLocation).

The reason for the Component (1st argument) is the variant use cases, for example:

  • you have a word document in your multimedia Component and want to publish it as a PDF file too, you can use the one of the overrides where you specify a binary array and change the filename (the extension part in this case).
  • You will now have the same multimedia Component published as two variants, so the usage counter of the Component can be correctly updated (making sure all the variants are removed when the Page(s) & Component(s) that use this are unpublished.

The URL of the published binary is correctly retreived from the outcome of the Engine.AddBinary() call, so in your case the value of the replace variable. But note, this value will be different when previewing and publishing. The example you mention is a result from a preview (either through the CME or the Template Builder). If you would debug your TBB using the Publisher on an aactual render/publish action, you would get the real URL the binary will get on the specific published target.

3
  • Thank you for your answer. Unfortunately when i try what you suggest,i get " No overload for method 'AddBinary' takes '2' arguments ". I checked the templating implementation manual TOM.NET (2009 SP1) and a few overrides are indeed possible but it does not work for me.
    – user1178
    Commented Sep 21, 2016 at 14:46
  • 1
    Ah sorry I did a quick check on the method in the Web 8 API guide, in 2009 there must be something similar, choose the one with the least amount of arguments ;). Commented Sep 21, 2016 at 15:11
  • this one worked : engine.AddBinary(component.Id, null, uri2, component.BinaryContent.GetByteArray(), "imageslide.jpg");
    – user1178
    Commented Sep 21, 2016 at 15:15
0

If all you're trying to do is publish a linked Multimedia Component, then the following should work:

Component linkedImage = (Component)engine.GetObject(linkedImageId);
if (linkedImage != null)
{
    Binary imageBinary = engine.PublishingContext.RenderedItem.AddBinary(linkedImage);
    string replacementUrl = imageBinary.Url;
}

(from one of our working TBBs)

Note that engine.AddBinary does not offer overrides. engine.PublishingContext.RenderedItem.AddBinary has multiple. At least it does in 2011, can't be sure about 2009 unfortunately.

1
  • i found a solution after Bart Koopman, but i will give your suggestion a try, thanks for helping !
    – user1178
    Commented Sep 21, 2016 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.