1

We have binaries placed under the following folder structure,

$(Publication)/Building Blocks/Content/DD4T Content/Multimedia/Images/xyz.jpg

When I use the following image file in the page, the URL gets created as “BuildingBlocks/content/DD4TContent/Multimedia/Images/xyz.jpg”, we actually need that to create as “/Images/xyz.jpg”.

In Binary Publisher class I have added the following code

    protected virtual void PublishItem(Item item, TcmUri itemUri)
    {
        Stream stream = null;
        if (item.Properties.ContainsKey("TemplateURI"))
        {
            TcmUri tcmUri = new TcmUri(item.Properties["TemplateURI"]);
        }

        try
        {
            string url;
            string publicationWebdavUrl = publication.WebDavUrl;
            Component mmComp = (Component)engine.GetObject(item.Properties[Item.ItemPropertyTcmUri]);
            //string text = mmComp.BinaryContent.Filename;
            //var extension = Path.GetExtension(text);
            Folder rootFolder = session.GetObject(publicationWebdavUrl + "/Building Blocks/System/Site Layout/Files") as Folder;
            StructureGroup parentSG = session.GetObject(publicationWebdavUrl + "/Root/Files") as StructureGroup;
            // <<Modified for IHS
            // >>Modified for IHS
            if (mmComp.ComponentType == ComponentType.Multimedia)
            {
                log.Info("Component Type : " + mmComp.ComponentType.ToString());
                parentSG = GetParentSG(mmComp, rootFolder, parentSG);
                targetStructureGroupUri = parentSG.Id;
                if (targetStructureGroupUri == null)
                {
                    this.log.Error("no structure group defined, publishing binary with default settings");
                    Component component = (Component)this.engine.GetObject(item.Properties["TCMURI"]);
                    this.log.Error(string.Format("publishing mm component {0} without variant id", component.Id));
                    Binary binary = engine.PublishingContext.RenderedItem.AddBinary(component);
                    url = binary.Url;
                    this.log.Error(string.Format("binary is published to url {0}", url));
                }
                else
                {
                    Component component = (Component)this.engine.GetObject(item.Properties["TCMURI"]);
                    string text = ConstructFileName(component, this.currentTemplate.Id);
                    var group = this.engine.GetObject(targetStructureGroupUri);
                    StructureGroup structureGroup = (StructureGroup)this.engine.GetObject(targetStructureGroupUri);
                    this.log.Error(string.Format("publishing Directory : {0} /n Publish Location Path : {1} /n Publish Location Url : {2} and Publish Path : {3}", new object[]
                    {
                        structureGroup.Directory,
                        structureGroup.PublishLocationPath,
                        structureGroup.PublishLocationUrl,
                        structureGroup.PublishPath
                    }));

                    stream = item.GetAsStream();
                    if (stream == null)
                    {
                        this.log.Error(string.Format("Cannot get item '{0}' as stream", itemUri.ToString()));
                    }

                    this.log.Error(string.Format("publishing mm component {0} to structure group {1} with variant id {2} and filename {3}", new object[]
                    {
                        component.Id,
                        targetStructureGroupUri.ToString(),
                        this.currentTemplate.Id,
                        text
                    }));

                    Binary binary2 = this.engine.PublishingContext.RenderedItem.AddBinary(item.GetAsStream(), text, structureGroup, this.currentTemplate.Id, component, component.BinaryContent.MultimediaType.MimeType);
                    url = binary2.Url;
                    this.log.Error(string.Format("binary is published to url {0}", url));
                }

                this.log.Error("binary published, published path = " + url);
                item.Properties[Item.ItemPropertyPublishedPath] = url;
            }

        }
        catch (Exception ex)
        {
            log.Info("Exception Thrown From Here.");
            throw new NullReferenceException(ex.Message);
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
            }
        }
    }    

What needs to be changed in this code to create folder structure as /Images/xyz.jpg. Thanks in advance.

Is there a way to edit the read only property of StructureGroup, which carries the publishingurl. I know i sound crazy, i even checked the Tridion.ContentManager.dll, it is taking from the currentstate. Since it dint work i came to a conclusion that, we need to place the binaries in the proper folder structure in Tridion itself. Does this conclusion sounds correct or please help me out.

1

It sounds like quite a few things are mixed up here.

There is a Template Building Block that ships with called "Publish Binaries for Page" which does this for you and publishes binaries with the Images URL set on Publication settings. You should not need to write any custom Template Building Block code to achieve your scenario.

Unless I misunderstand your question or scenario, I would review your set up to ensure you are using the DD4T TBBs on your Page Templates.

See the following URLs for guides on setting up DD4T

| improve this answer | |

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.