how can i add new page in pages application

Muhammad Kaleem asked on March 26, 2019 13:43

can anyone explain about how can i create new page in pages application programatically (want to add a new page under news page and populate data in fields), any help will be highly appreciated

Recent Answers


Michael Legacy answered on March 26, 2019 15:35

Hey Muhammad. There are some API examples for building pages programatically here: Pages API Examples

The following link will give you some insight into working with already created pages within the API: Working with pages in the API

Then hidden in the documentation above is a method on the TreeNode object call "SetValue". So within your new page object you would do something like this:

newPage.SetValue("propertyName", value);

So all together a new page object would look something like this:

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets the current site's root "/" page, which will serve as the parent page
    TreeNode parentPage = tree.SelectNodes()
        .Path("/")
        .OnCurrentSite()
        .Culture("en-us")
        .TopN(1)
        .FirstOrDefault();

    if (parentPage != null)
    {
        // Creates a new page of the "CMS.MenuItem" page type
        TreeNode newPage = TreeNode.New(SystemDocumentTypes.MenuItem, tree);

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

        // Sets custom properties of the new page
        newPage.SetValue("property1", "test");
        newPage.SetValue("property2", DateTime.Today);
        newPage.SetValue("property3", Array);

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

Michael Legacy answered on March 26, 2019 15:39

Also, here is the codex reference for the TreeNode class methods within the CMS.DocumentEngine Namespace. You can review all the methods (and their overloads) you have access to when creating or updating TreeNode objects.

TreeNode methods

1 votesVote for this answer Mark as a Correct answer

Muhammad Kaleem answered on March 27, 2019 05:48

Thanks Michael Legacy

0 votesVote for this answer Mark as a Correct answer

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