0

If a page exists in particular location, that page must contain a particular schema component. If not create a new component and add the same to the page. We thought of implementing the same via Event Handlers. I am getting the following error in my else part.

Tridion.ContentManager.InvalidXmlException: Unable to find http://www.abc.com/tridion/schema/article:Metadata. at Tridion.ContentManager.IdentifiableObject.AssertValidRootContentElement(XmlElement rootElement, XmlSchemaSet schemaSet, XmlQualifiedName expectedRootElementName)

public void OnPageSave(Page page, SaveEventArgs args, EventPhases phases)

{

        using (new Tridion.Logging.Tracer())
        {
            try
            {
                if (page == null) return;
                string path = page.WebDavUrl;
                Logger.WriteWarning("Page Path:"+path, ErrorCode.GENERAL);  
                if (path.Contains("/webdav/040English/Home/about/press/"))
                {                        
                    IList<ComponentPresentation> cpList = page.ComponentPresentations;
                    StringBuilder CT= new StringBuilder();
                    if (cpList != null)
                    {
                        foreach (ComponentPresentation cp in cpList)
                        {                                
                            CT.Append(cp.ComponentTemplate.Title);
                            CT.Append(", ");
                        }                            
                        if (!CT.ToString().Contains("Article"))
                        {                                                             
                            using (Session adminSession = new Session(WorkflowConstant.NT_AUTHORITY_SYSTEM))
                            {
                                string compPath = "/webdav/{0}/Building%20Blocks/Content/About/Press/2014/{1}.xml";                                    
                                if (adminSession.IsExistingObject(string.Format(compPath,page.ContextRepository.Title, page.Title)))
                                {
                                    Logger.WriteWarning("yes Existing", ErrorCode.GENERAL);
                                    Component c1 = adminSession.GetObject(string.Format(compPath, page.ContextRepository.Title, page.Title)) as Component;                                       
                                    string web1 = "/webdav/{0}/Building Blocks/System/Component Templates/Shared Public/Body_Article.tctcmp";
                                    ComponentTemplate cpTemplate = adminSession.GetObject(string.Format(web1,page.ContextRepository.Title)) as ComponentTemplate;                                      
                                    page.ComponentPresentations.Add(new ComponentPresentation(c1, cpTemplate));                                       
                                    page.Save();                                        
                                    page.CheckIn();                                        
                                }
                                else {                                        
                                    Component c2 = new Component(adminSession, new TcmUri("tcm:182-10631-2"));   -->(FolderID)                                              c2.Schema= new Schema(new TcmUri("tcm:182-2272-8"),adminSession); --// Allowed Schema is Article in the particular Folder
                                    c2.Title = page.Title;
                                    Logger.WriteInfo("a4 c2 Title");                                       
                                    c2.Save();
                                    c2.CheckIn(); 
                                }  
                            }
                        }
                    }
                }

            }
  • 1
    -32 is the Item Type code for a Component Template... Not sure what you're trying to achieve here. – Nuno Linhares Jul 3 '14 at 13:46
  • 2
    None of your snippets calls Save, so the new Component will not be saved. But as the others below say, the TOM.NET is intended to be a read-only API for use in Templates. For creating/updating content you're better off using the Core Service API. – Frank van Puffelen Jul 3 '14 at 16:04
  • +1 to Frank's comment and John's point #4 below. But if you need an item at a specific step in publishing, then the Event System would be a better fit over the Core Service. Bart Koopman describes how to handle integrations and which APIs to use in this TridionWorld post. But if related to automatic page or content creation, Experience Manager's Page Type feature is the easiest way to create, maintain, and configure such automation (except for folder creation). – Alvin Reyes Jul 3 '14 at 18:19
3

To get a solid answer you should paste your code with your question and the error message you have.

That said, here's the basic principle for creating a new component using Tom.Net:

Component myNewComp = new Component();
myNewComp.Title = "hello";
myNewComp.Schema = {your schema object};
myNewComp.Content = {An XElement object containing your component Content};
myNewComp.OrganizationalItem = {your folder object}
myNewComp.Save(true);

Some notes:

  1. You can also set metadata if need be, this is the same process as setting the .Content property of the component (use .Metadata)

  2. Your schema and organisational item (folder) object references must match the same publication ID where you're saving the component.

  3. Creating the component content XElement must match the XSD schema. I'd always double check the XML content by reading an existing component based on the same schema to ensure it's correct.

  4. Most importantly! - The TOM.NET API is read only by design, whilst it can be enabled, it's disabled for security reasons. I'd recommend reviewing your need to create a component at render time and look at using the Core Service as part of a custom extension to perform the task of creating content.

0

Try following

var adminSession = engine.GetSession();
var comp = new Component(adminSession, new TcmUri("tcm:11-108-2)); //folder id
comp.Schema= new Schema(new "tcm:11-10-8));
comp.Title = "tom component";
// set other fields
comp.Save();  
comp.CheckIn(true);

By default tom.net is readonly in templating, please Make sure templating is not readonly via ensuring setting from following link(login required)

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.