13

I have a TBB.Net that insert some items into the package. I need that this TBB.Net inserts all the items in a List of items and then insert this list in the package, but with the PushItem method I can't push a List of Items.

I try something like this:

for(...)
{
    _item = package.CreateStringItem(ContentType.ComponentArray, 
                                     ComponentPresentationList.ToXml(_list));
    BodyItems.Add(_item);
    _item = package.CreateStringItem(ContentType.Text, cont.ToString());
    BodyNumItems.Add(_item);
}
package.PushItem("Body", BodyItems);
package.PushItem("Body_num", BodyNumItems);

But PushItems only accept Items.

Some way to do this ?

2 Answers 2

13

Basically a DWT Template allows you to iterate over arrays, but in the Package there is only one sort of array we can push ourselves, which is the Component Presentation array (which can contain Component Template TCMURIs, but also only Component TCMURIs).

Through this you can push (what I refer to as) a Dummy Component Array in the package which you can use to loop over and then through the double lookup feature, to make it work like looping over a set of Package variables.

For example, you push in the package a Component Array with three dummy TCMURIs (they can all be tcm:0-0-0, it doesn't matter what value they have). Then you create three package variables named: Var_0, Var_1, Var_2. Now in your DWT Template you can use:

<!-- TemplateBeginRepeat name="MyDummyArray" -->
  @@Var_${TemplateRepeatIndex}@@
<!-- TemplateEndRepeat -->

Which will result you in the values of your three package variables.

Unfortunately there is no way to create a string array directly or a field array for that matter, so for everything you will want to loop over, you will have to use an existing field array, or create a "Dummy Component Array".

In your .Net assembly TBB you create the Component Array as follows:

ComponentPresentationList cpList = new ComponentPresentationList();

// add dummy uris in the array (as many times as you need)
// using Tridion.ContentManager.Templating.ComponentPresentation
cpList.Add(new ComponentPresentation(TcmUri.UriNull));
cpList.Add(new ComponentPresentation(TcmUri.UriNull));
cpList.Add(new ComponentPresentation(TcmUri.UriNull));

// push the array in the package
Item myDummyCPs = package.CreateStringItem(ContentType.ComponentArray, cpList.ToXml());
package.PushItem("MyDummyArray", myDummyCPs);

// and then push the values in the package too
package.PushItem("Var_0", package.CreateHtmlItem("first value"));
package.PushItem("Var_1", package.CreateHtmlItem("second value"));
package.PushItem("Var_2", package.CreateHtmlItem("third value"));

Update:

To react on the comment that this method could become to large for a high number of variables, with a little refactoring that shouldn't be an issue. Also the Package can contain quite a lot of variables and their values are usually quite small (I haven't seen errors related to having too much items in the package yet). I'm thinking along the lines of:

ComponentPresentationList cpList = new ComponentPresentationList();

// using Tridion.ContentManager.Templating.ComponentPresentation
for (int i = 0; i < count; i++)
{
    // add a dummy uri to the array
    cpList.Add(new ComponentPresentation(TcmUri.UriNull));
    // push the value in the package
    package.PushItem(string.Format("Var_{0}", i), 
                     package.CreateHtmlItem("get the value from some array using [i]"));
}

// push the array in the package
Item myDummyCPs = package.CreateStringItem(ContentType.ComponentArray, cpList.ToXml());
package.PushItem("MyDummyArray", myDummyCPs);
2
  • I've been using this way but the number of Var is variable depending of Page and when "Vars" are more than 20, the method is too large. Thank you for answer.
    – tridionero
    Commented Apr 10, 2013 at 14:35
  • yeah unfortunately there are limitations to this approach, would have been cool if we could have pushed all sorts of arrays in the package, perhaps a valid request for ideas.sdltridion.com Commented Apr 10, 2013 at 15:29
4

You may Try the Package.CreateComponentUriListItem to have a list of TCM URIs of all items. And then using the Package.PushItem to insert the desired list into the stack.

1
  • Make sense Pankaj : List<TcmUri> dummyUriList = new List<TcmUri>() { }; for (int cnt = 0; cnt < colFldCnt.Count(); cnt++) { dummyUriList.Add(new TcmUri(TcmUri.UriNull)); } m_Package.PushItem("varName", m_Package.CreateComponentUriListItem(ContentType.ComponentArray, dummyUriList)); Commented May 29, 2015 at 0:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.