Create a page with the Fluent API
Use this procedure and the code sample to create pages via the fluent API.
- Get the parent node.
If you do not specify a parent node, you must use theFrontendRootNodeId. - Check whether a page with the specified ID already exists.
To do this, you get all page nodes using the plural page facade and LINQ query. - Create the page.
If the page ID does not exist, you create the page node using theCreateNewStandardPagemethod of the facade. To create the page data, you use thePageproperty of the node. - Set the properties of the page.
We recommend to set at least the following properties:-
Properties of the
PageNodeobject:NameTitleDescriptionShowInNavigation
-
Properties of the
PageDataobject:TitleHtmlTitleDescription
-
- If required, set the page as homepage.
To set the page as home page, you use theSetAsHomePagemethod of the singular page facade. - Save the changes.
- Publish the page.
To publish the page in live state, call theMessageWorkflowmethod of theWorkflowManagerclass and pass the required parameters.
EXAMPLE: In the following code example, the
CreatePageFluentAPImethod create a new standard, empty, frontend page:
C#
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Workflow;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Pages
{
public partial class PagesSnippets
{
public static void CreatePageFluentAPI(Guid pageId, string pageName, bool isHomePage, Guid parentPageNodeId)
{
var pageDataId = Guid.NewGuid();
// Get the parent node Id
if (parentPageNodeId == Guid.Empty)
{
parentPageNodeId = SiteInitializer.CurrentFrontendRootNodeId;
}
// Check whether exists
var count = 0;
App.WorkWith().Pages().Where(p => p.Id == pageId).Count(out count);
if (count == 0)
{
// Create the page
App.WorkWith().Page()
.CreateNewStandardPage(parentPageNodeId, pageId, pageDataId)
.Do(p =>
{
p.Title = pageName;
p.Name = pageName;
p.Description = pageName;
p.ShowInNavigation = true;
var pd = p.GetPageData();
if (pd != null)
{
pd.HtmlTitle = pageName;
}
})
.Publish()
.SaveChanges();
// Check whether home page
if (isHomePage)
{
App.WorkWith().Page(pageId).SetAsHomePage().SaveChanges();
}
}
}
}
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.