Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images are getting published with TCM id appended with the image name

Mode of publishing - static

I'm trying to publish images, but the issue is whenever I publish those images, their TCM URI is appended to their name (i.e if image name is example and its TCM URI is like tcm:1-115, image filename becomes example_tcm1-115).

I have written the following code:

public void Transform(Engine engine, Package package)
{
    Filter MMCompFilter = new Filter();
    MMCompFilter.Conditions["ItemType"] = Tridion.ContentManager.ItemType.Component;
    Folder folder = engine.GetObject("tcm:1-1-2") as Folder;

    foreach (Component MMcomp in folder.GetItems(MMCompFilter))
    {
        Binary binary = engine.PublishingContext.RenderedItem.AddBinary(MMcomp);
        String binaryurl = binary.Url;
        char[] array = binaryurl.ToCharArray();
        Array.Reverse(array);
        string obj = new string(array);
        string final = newImagepath(obj);
        char[] array2 = final.ToCharArray();
        Array.Reverse(array2);
        string obj2 = new string(array2);

        package.PushItem("Image", package.CreateHtmlItem(obj2));
    }

    public string newImagepath(string filePath)
    {
        int formatIndex =filePath.IndexOf(".");
        string format= filePath.Substring(0,formatIndex);
        int finalPath=filePath.IndexOf("_");
        string newPath=filePath.Substring((finalPath+1));
        return (format+"."+newPath);
    }
}

I want to publish images without the TCM URI appended to it. Plz suggest how can it be done.

like image 959
user1573378 Avatar asked Aug 03 '12 07:08

user1573378


3 Answers

Chris Summers wrote a very nice article on this very topic http://www.urbancherry.net/blogengine/post/2010/02/09/Unique-binary-filenames-for-SDL-Tridion-Multimedia-Components.aspx

It is basically a very simple thing to fix, but can have huge consequences which you should be aware of!

You can only publish a binary with a certain file-name in a single location once (and a binary can only be published to a single location on the presentation server, unless you publish it as a variant). However, in the CMS it is very easy to create Multimedia Components with the same binary file-name in different folders, which if they get published to the same location will be in conflict. That is why by default SDL Tridion appends the TCM URI to the filename to make it unique.

like image 131
Bart Koopman Avatar answered Nov 20 '22 08:11

Bart Koopman


Simplest is always best.

In your TBB, just push the individual images to the package:

package.PushItem(package.CreateMultimediaItem(component.Id));

Then use the "PublishBinariesInPackage" TBB to publish these images to your presentation server.

like image 38
Nuno Linhares Avatar answered Nov 20 '22 07:11

Nuno Linhares


You can use the RenderedItem.AddBinary method for this goal. Some of the overloaded versions of the method allows to publish an image as a stream, and pass any file name. For example:

public Binary AddBinary(
    Stream content,
    string filename,
    string variantId,
    string mimeType
)
like image 3
Viachaslau Kamkou Avatar answered Nov 20 '22 08:11

Viachaslau Kamkou