5

I am writing a C# fragment (to be used in a Page Template) and want to push a Page Metadata multivalued text field as an arraylist in the package.

After this I have to loop through this arraylist in my Dreamweaver template to display its content.

Is it possible? if yes, please share some code snippet. I came up with the code below, but not sure if I am on the right track:

<%@ Import Namespace="System.Diagnostics"%>
<%@ Import Namespace="Tridion.ContentManager.ContentManagement.Fields"%>
<%@ Import Namespace="System.Collections.Generic"%>
<%@ Import Namespace="System.Linq"%>

Page page = this.GetPage();
ItemFields itemFields = new ItemFields(page.Metadata, page.MetadataSchema);

TextField textField = (TextField)fields["page.article_title"];

foreach (ItemFields field in textField.Values )
{
    log.Info( "pixelcode = " + _field.Value );
}

// Here I'm confused on how to push the arraylist into the package
// So I'm planning to push each value separately in the package
package.PushItem("???");
1
5

There is a work around but it will implicate to add several items to the package.

C# code.

IEnumerable<string> values = ((TextField)itemFields["MultiValueField"]).Values;

IEnumerable<TcmUri> fakedTcmList = values.Select(s => TcmUri.UriNull);
package.PushItem("FakedList", package.CreateComponentUriListItem(ContentType.ComponentArray, fakedTcmList.ToList()));

int index = 0;
foreach (string value in values) {
    package.PushItem("FakedListItem" + index++, package.CreateStringItem(ContentType.Text, value));
}

Dreamweaver code:

<!-- TemplateBeginRepeat name="FakedList" -->
    @@FakedListItem${TemplateRepeatIndex}@@
<!-- TemplateEndRepeat -->
5

Why would you want to push these values into the package? In your Dreamweaver Template you can directly loop over the Metadata values of your Page:

<ul>
  <!-- TemplateBeginRepeat name="Page.Metadata.article_title" -->
    <li>@@Field@@</li>
  <!-- TemplateEndRepeat -->
</ul>

There is no need to push Metadata field values into the package unless you need to do something with them beforehand. Although simple string or numerical operations could also be done directly in your DWT TBB.

If you wanted to use the Metadata of a Page inside a Component Template, then you have a bit more of a challenge. Indeed as answered by me here, it is not possible to push array lists into the package yourself (directly that is, my answer gives the workaround). But you can consider to push your Page into the package of your Component Template. This will allow you to access the fields directly the same way as you have them a Page Template.

But please be warned, when pushing a Page object into the package of a Component Template, it might be confusing for other Template Building Blocks to see what you are dealing with. I always check if the is a Page object in the Package to check if I'm dealing with a Page- or a Component Template, this would then give me an incorrect result.

The C# code for adding the Page to the package will look something like this:

package.PushItem("Page", package.CreateTridionItem(ContentType.Page, engine.GetObject("tcm:6-221-64")));

After this the above DWT snippet will do the job again. But, this is only needed when you want to do this in Component Template, in your given example you wanted to use it directly on a Page Template, so you only need the DWT code and no additional C# Fragment or Assembly TBBs needed.

2
  • Thanks Bart for answering here. I need C# fragment because this field in the page metadata is used to place some scripts(javascripts) and C# fragement have the code to unescape the HTML tags.
    – user584
    Jul 30 '13 at 6:53
  • Then you might want to consider using a Custom Function (link requires login) to unescape, or you will have to push in the values separately as explained in my workaround answer in the other question. Jul 30 '13 at 7:51
3

It's not possible at the moment, please refer to answer provided by bart here for a workaround

1

Package is actually a collection of stacks: hence the use of the word Push in the method name. In your case, a lot depends on how you intend to extract the data from the Package and use it. So you could Push repeatedly on to the same Item in your Package. Unfortunately there isn't a Pop method, so you'd have to "Get" the item using the Remove method. Again, unfortunately, Remove returns the name of the item, which you probably don't want in your output, so using this technique directly in raw Dreamweaver syntax is going to be clumsy, so you might consider implementing a Pop method as a Function Source.

Alternatively, you might consider doing all the processing in your first step, so that you Push your desired output to the Package in one go.

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.