Skip to content

Instantly share code, notes, and snippets.

@sitefinitySDK
Last active October 14, 2024 12:03
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Workflow;
namespace SitefinityWebApp
{
public partial class PagesSnippets
{
public static void CreatePageNativeAPI(string pageName, bool isHomePage, Guid parentPageNodeId)
{
// If you want to perform an action on a specific site in a multisite deployment,
// you can use the CurrentSite property, accessible from SystemManager.CurrentContext.MultisiteContext:
var currentContext = SystemManager.CurrentContext;
var multisiteContext = currentContext.MultisiteContext;
var currentSite = multisiteContext.CurrentSite;
using (new SiteRegion(currentSite))
{
PageManager manager = PageManager.GetManager();
if (parentPageNodeId == Guid.Empty)
{
parentPageNodeId = SiteInitializer.CurrentFrontendRootNodeId;
}
PageNode parent = manager.GetPageNode(parentPageNodeId);
var pageId = Guid.NewGuid();
//Create the page node
PageNode pageNode = manager.CreatePage(parent, pageId, NodeType.Standard);
//Create the PageData and set its properties
PageData pageData = pageNode.GetPageData();
pageData.HtmlTitle = pageName;
//Set Page properties
pageNode.Name = pageName;
pageNode.Description = pageName;
pageNode.Title = pageName;
pageNode.ShowInNavigation = true;
pageNode.ApprovalWorkflowState = "Published";
if (isHomePage)
{
SystemManager.CurrentContext.CurrentSite.SetHomePage(pageId);
}
//Commit the page to DB
manager.SaveChanges();
//Publish the page via workflow
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageNode.Id, typeof(PageNode), null, "Publish", false, bag);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment