Assign Template while creating new page in Kentico 13 via API.

Vishal Bhatt asked on August 24, 2022 11:55

I am creating a new page via Kentico API. But I am not able to assign any template while creating new page. Is there any solution in Kentico API to create a new page with an existing template?

Recent Answers


Eugene Paden answered on August 24, 2022 15:36

you may need to create the page and then assign a template to it.

0 votesVote for this answer Mark as a Correct answer

Vishal Bhatt answered on August 24, 2022 15:43

Thank you for your response @Eugene Paden. But I am creating a page via API and need to assign a template from API, not from the Admin panel.

0 votesVote for this answer Mark as a Correct answer

Eugene Paden answered on August 24, 2022 17:28

That's what I meant, I've been searching on the API reference how to assign a Template to a page and so far, this is the only thing i've come across PageInfo API

0 votesVote for this answer Mark as a Correct answer

Vishal Bhatt answered on August 25, 2022 09:52 (last edited on August 25, 2022 10:02)

@Eugene Paden I am using the below code for Kentico13 page creation. Here I need to assign the existing page template.

    // Gets the current site's root "/" page, which will serve as the parent page
            TreeNode parentPage = new DocumentQuery<TreeNode>()
                                        .Path("/", PathTypeEnum.Single)
                                        .TopN(1)
                                        .FirstOrDefault();

            if (parentPage != null)
            {
                // Creates a new page of the custom page type
                TreeNode newPage = TreeNode.New("PageType.PageMenuItem");

                // Sets the properties of the new page
                newPage.DocumentName = "TestPage";
                newPage.DocumentCulture = "en-us";

# Code #

**//? how to assign PageTemplate to newPage here?**

                // Inserts the new page as a child of the parent page
                newPage.Insert(parentPage);
            }
0 votesVote for this answer Mark as a Correct answer

Sean Wright answered on September 10, 2022 18:03

Since Page Templates are part of the MVC application (and not the CMS) APIs to interact with things like Page Templates are not available in Xperience's shared libraries since these are used in both the CMS and live site applications.

Additionally, programmatically setting a Page Template isn't considered a standard use case for Xperience, so there actually aren't any publicly accessible APIs to set a Page Template on a TreeNode.

That said, Xperience doesn't prevent you from setting one!

if (parentPage != null)
{
    // Creates a new page of the custom page type
    TreeNode newPage = TreeNode.New("PageType.PageMenuItem");

    // Sets the properties of the new page
    newPage.DocumentName = "TestPage";
    newPage.DocumentCulture = "en-us";

    var pageTemplateConfig = new
    {
        identifier = "Your.PageTemplateIdentifier",
        properties = null // You can specify properties here if your Page Template has them
    };

    var serializerSettings = new JsonSerializerSettings 
    { 
        ContractResolver = new CamelCasePropertyNamesContractResolver() 
    };

    newPage.SetValue(
        "DocumentPageTemplateConfiguration", 
        JsonConvert.SerializeObject(pageTemplateConfig, serializerSettings));

    // Inserts the new page as a child of the parent page
    newPage.Insert(parentPage);
}

How you populate the Page Template configuration really depends on where this code is executing. If it's in a library shared between the CMS and live site (the most likely situation), it's up to you to maintain the properties of the Page Template configuration manually, making sure it has the correct property names and values to match the IPageTemplateProperties of the Page Template assigned to the identifier.

I typically set properties to null when I can because it simplifies the code maintenance over time. But sometimes you need to pre-populate a new page with a specific configuration, in which case you need to keep it aligned with the shape of the IPageTemplateProperties for the Page Template.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.