I have a code Component and I need to edit the content and save the Component through a C# TBB. How can we save the edited content in a Component?

XmlDocument XmlDoc = new System.Xml.XmlDocument();
Component comp = GetComponent();            
if (comp.MetadataSchema.ToString().ToLower() == "testcodeschema")
    XmlDoc.LoadXml(Regex.Replace(comp.Content.ToString(), @"[$,]", string.Empty));
    //now need to save the component with the edited content.
  • What effort have you put into solving this yourself, for example: research/reading, code you've tried, etc? Apr 3, 2017 at 8:00
  • @NickoliRoussakov, i tried searching through the content available on net but didn't find anything related to saving a component through a C# tbb. I already achieved this by using core services and through event handler on save event. but just trying to find out can we achieve component save using a c# tbb. Apr 3, 2017 at 8:17
  • It's great that you put some effort in before posting your question. Make sure that you show/explain what you've tried so other know that you're not a lazy guy trying to get others to do your job :) Here is a link on how to ask: stackoverflow.com/help/how-to-ask Apr 3, 2017 at 8:19
  • usually i will ask any question with doing ground work on it, but i will take care that i will put my finding too with questions next time. thank you @Nickol Apr 3, 2017 at 9:17

1 Answer 1


I sense that what you're trying to do is replace some invalid characters while publishing. You would typically do this on the Package.Output variable, not on the Component itself. Output is the text that gets published as part of the Component Presentation.

You typically don't want to modify the Component while publishing it. Before is fine, after is fine, but during is weird. Here is why: when you're doing this logic inside of a TBB, you are within a rendering/publishing session for the latest version of the Component. So any changes/saves you make to the Component within the publishing session, will create a new version of the Component, which will not be the version that's within your current session.

There is also a protective mechanism in the TOM.NET API that prevents saving Components within the publish session (probably for this reason). In the Tridion.ContentManager.config the setting is allowWriteOperationsInTemplates and it is off by default.

If you still really want to save a Component with your TBB using your approach. Place your updated Component XML back into comp.Content. Then call comp.CheckIn(). Otherswise, use the Event System and do this on the Component Save event.

For reference also see: Is it possible to assign or change the value of a component Field during templating?

  • +1 on the remark to use an event handler to replace the invalid characters, which could be done on save, to make it a clear use case. Apr 3, 2017 at 11:07
  • 1
    Indeed, templates are supposed to render the current state of items, not change their state. Rendering happens in a special security mode (internally referred to as *trusted read-only mode") which allows read access to everything (regardless of the permissions of the user who performs the rendering) but does not allow any kind of "write" operation. Apr 5, 2017 at 6:30

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.