I was trying to push content from C# Fragment using below snippet

Item componentItem = package.GetByType(ContentType.Component);
Component component= engine.GetObject(componentItem.GetAsSource().GetValue("ID")) as Component;
ItemFields fields = new ItemFields(component.Content, component.Schema);
TextField text = fields["text"] as TextField;
string description = text.Value;
log.Info("Before Push:"+description);
package.PushItem("Output", package.CreateHtmlItem(description));

On Debugging:

CSharpSourceTemplate: Before Push:

<a href="&amp;=" xmlns="http://www.w3.org/1999/xhtml">&amp;Check</a>
<strong xmlns="http://www.w3.org/1999/xhtml">Check</strong>

CSharpSourceTemplate: Output:

<a href="&" xmlns="http://www.w3.org/1999/xhtml">&amp;Check</a>
<strong xmlns="http://www.w3.org/1999/xhtml">Check</strong>

I have tried all different content type in package.CreateStringItem method, but attribute value is encoding from &amp; to &

Due to this encoding my XML is breaking, have any faced this issue earlier?

  • I'm confused. Bart's answer says he can't reproduce your problem and you accepted the answer. Did you figure out what the problem was? If so, please expain. – Rick Pannekoek Jul 12 '17 at 6:15
  • removed all other TBB's and verified the output parameter as bart did ... there are another bunch of compound templates other than default action which might cause decoding. Verifying exactly which TBB encoded – Kumar_diru Jul 12 '17 at 6:21

If I remember correctly when using Package.CreateHtmlItem(htmlContent) (which is similar to using Package.CreateStringItem(ContentType.Html, htmlContent)) then your content will be automatically HTML encoded.

So I would recommend using Package.CreateStringItem(ContentType.Text, htmlContent) instead if the automatic HTML encoding is in your way. But be aware that if you use the Default Finish Actions TBB in your Compound Template, there is a Convert XML to HTML TBB at the end there which tries to convert the XML of the package item called Output into HTML. This also does some encoding which might be unwanted, and you might need to remove it from the TBB pipeline.


I tried using the following code in a C# TBB in SDL Web 8.5

string description = "<a href=\"&amp;\">&amp;Check</a>";
package.PushItem("HtmlOutput", package.CreateHtmlItem(description));
package.PushItem("Html2Output", package.CreateStringItem(ContentType.Html, description));
package.PushItem("TextOutput", package.CreateStringItem(ContentType.Text, description));

And the outcome in the Template Builder is:


<a href="&amp;">&amp;Check</a>


<a href="&amp;">&amp;Check</a>


<a href="&amp;">&amp;Check</a>

So other than a dfference in the type of the package items (text/html or text/plain), I don't see any HTML encoding happening at all actually.

Which makes me think that maybe you need to check the line TextField text = fields["text"] as TextField; and validate that this field is in fact a TextField and not an XhtmlField (a.k.a. Format Area).

Update 2

Web 8 shows me exactly the same as Web 8.5, see the screenshot below

enter image description here

  • Hi Bart, Thanks for Reply... Yes I tried using Package.CreateStringItem(ContentType.Text, htmlContent), but in Template Builder I can see Output parameter is still in encoded format. – Kumar_diru Jul 11 '17 at 13:29
  • @Kumar_diru as mentioned, it is important at what step you look at the Output parameter, maybe you can edit your question and supply a few more details about the Template and if the Output is taken directly after executing your C# Fragment or if it is at the end of the line. – Bart Koopman Jul 11 '17 at 13:41
  • Hi Bart, I consume parameter directly after executing C# Fragment – Kumar_diru Jul 11 '17 at 13:46
  • @Kumar_diru see my updated answer, I don't see this happening in SDL Web 8.5 – Bart Koopman Jul 11 '17 at 14:09
  • Hi bart. Thanks for updates. current version of Mine was Web 8 . In my question if we observe Before Push log has encoded value but upon push it got decoded. – Kumar_diru Jul 11 '17 at 14:23

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.