1

I'm trying to publish multimedia content (images) on my server from a C# Template Building Block.

I tried the following:

// 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));

             TcmUri uri2 = new TcmUri("tcm:126-3103-4");
             StructureGroup sg = new StructureGroup(engine.GetSession(),uri2);
           string replace = engine.PublishingContext.RenderedItem.AddBinary(component, sg).Url;
        }
    }
}

uri2 is the TCMUri of a Folder where I want the image to be published on the server and component is the Component containing the image.

With that code, I get the following error using Template Builder:

Invalid context Publication specified in URI: New StructureGroup (parent: tcm:123-3744-2). Expecting: 123.

2 Answers 2

2

In your StructureGroup constructor call you are passing it a TCM URI of a Folder (item type 2), but the method requires the TCM ID of the parent Structure Group where you want to create the new one, I.e. Item type 4). See here for a reference of item types: https://tridion.stackexchange.com/a/3015/159

5
  • Thank you for answering. How can i find this parent structure group ?
    – user1178
    Commented Sep 21, 2016 at 11:36
  • Those purple colored nodes. They resemble directories of where things get published on the server. Pick one where you want your binary to live. (Also don't forget to up-vote the answer :) Commented Sep 21, 2016 at 11:44
  • after many checks, i still have the same error : "Invalid context Publication specified in URI: New StructureGroup (parent: tcm:126-3103-4). Expecting: 123." I have updated my question with the full block of code ....
    – user1178
    Commented Sep 21, 2016 at 13:15
  • You seem to miss the fact that the code is trying to create a Structure Group, rather than opening an instance to an existing one which is what is required for the AddBinary call Commented Sep 23, 2016 at 8:47
1

There are two overloads for the StructureGroup constructor:

StructureGroup(Session, TcmUri) - Create an instance for a new Structure Group.

StructureGroup(TcmUri, Session) - Create an instance for an existing Structure Group.

You are using the first constructor overload, which tries to create a new Structure Group, but in a publishing context, I gather that you actually meant to use the second overload of the constructor, so change the order of your input parameters.

Also in your original question you were using the following code:

TcmUri uri2 = new TcmUri("tcm:123-3744-2");

Which is a TCMURI of a Folder (ending on -2, which identifies it as Item Type Folder). You later changed it to end on -4, which is indeed the correct Item Type for a Structure Group.

The error message seems to identify another issue, namely that you are trying to publish a multimedia Component from Publication 123, why specifying a Structure Group of a different (child) Publication. You have to keep all the URIs in the same context Publication.

Your Answer

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