3

In certain cases, we want to create pages automatically using the event system. While doing that we're running into an issue with certain pages that have a Region.

We're succeeding in creating and saving pages that have no regions, but whenever we try to create and save a page with a region 'Main' we get an error stating that there is no region 'Main'.

Our code:

Our code

We're 100% sure the NestedRegion we're getting from the RegionSchema is the 'Main' region with the correct RegionSchema.

1

1 Answer 1

8
  1. When you are trying to create a page with regions, Tridion would give a notification asking you to update the set of nested Regions to make it valid. In the Event System code, you would want to perform this update by calling the SynchronizeWithSchema method on the page. enter image description here

  2. You would also have to set the page metadata schema with the correct root element namespace.

  3. Instead of adding the region to the page, just add the component presentations on the page regions directly. Since you would be setting the page template to the page and the template contains the page schema regions, just adding the component presentations should suffice.

So, here is an example code that creates a page with a component presentation added to the page's first region through the Event System.

// Component Presentation to be added to the page.
Component component = (Component)subject.Session.GetObject("tcm:5-305-16");
ComponentTemplate componentTemplate = (ComponentTemplate)subject.Session.GetObject("tcm:5-67-32");
ComponentPresentation componentPresentation = new ComponentPresentation(component, componentTemplate);

PageTemplate homePageTemplate = (PageTemplate)subject.Session.GetObject("tcm:5-77-128");
Schema homePageSchema = (Schema)subject.Session.GetObject("tcm:5-76-8");

// Create a new page.
Page newPage = new Page(subject.Session, new TcmUri("<TCM ID of the structure group where page is to be created>"));

newPage.Title = "New page";
newPage.FileName = "test";
newPage.PageTemplate = homePageTemplate;
newPage.MetadataSchema = homePageSchema;

// Set the page metadata with the right root element namespace. 
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Metadata xmlns=\"uuid:122e776e-b53b-47ef-b51e-8c4242716a7f\" />");
XmlElement metadataElement =  doc.DocumentElement;
newPage.Metadata = metadataElement;

// Synchronize metadata with the metadata schema. 
newPage.SynchronizeWithSchema(SynchronizeFlags.All);

// Add a CP to the first region. 
newPage.Regions[0].ComponentPresentations.Add(cp);

newPage.TryCheckOut();
newPage.Save(true);
4
  • The problem I am running into is that after following all these steps, I get a page with a RegionSchema that shows 1 NestedRegion named "Main" in its RegionDefinition but the page still has 0 regions.
    – Christel
    Nov 9, 2021 at 19:49
  • Do you see 1 nested region in the code and the page in the CM shows 0 regions? Nov 9, 2021 at 23:29
  • When debugging, if I look at the new page object I see 1 NestedRegion in page.RegionSchema.RegionDefinition.NestedRegions and zero reions in page.Regions
    – Christel
    Nov 10, 2021 at 8:23
  • Is this after you have called SynchronizeWithSchema? Does the page template that you use on the page have regions defined? The code example as such should work provided right TCM IDs are used and the page template has regions defined. Nov 10, 2021 at 13:39

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.