Creating pages (6.3 and below)
To create a page you can use the Native API or the Fluent API.
When creating a page, you must perform the following:
-
Get the parent node Id.
When creating the page, you must specify the parent node. If you do not have parent node, you must use the front-end root node if the page is front-end page and the back-end root node if the page is back-end page. You use SiteInitializer to get the Ids of the root nodes.
-
Check whether the page already exists.
Before you create the page, you must check whether it already exists.
-
Create the page.
If the page does not exist, you create a new page with the specified Id and parent. You must create the page node and page data and then, you assign the page data to the Page property of the node.
-
Set the required properties.
When creating a new page, it is recommended to set at least the following properties:
- Of the page node:
- Title
- Name
- Description
- UrlName
- ShowInNavigation
- DateCreated
- LastModified
You can also set any other properties in this step.
-
Of the page data:
- Title
- HtmlTitle
- Description
You can also set any other properties in this step.
-
Set the page as home page, if needed.
After you create the page, you must set it as home page if it is the initial page of the web site. You set the page as home page if it contains the main information in the web site.
-
Save the page.
Save all changes that you have made to the page.
-
Publish the page.
Finally, you publish the page using the workflow manager.
NOTE: In the code examples below the ID argument is assigned to the master version of the page.
Native API
public
static
void
CreatePageNativeAPI(Guid pageId,
string
pageName,
bool
isHomePage, Guid parentPageNodeId)
{
var pageDataId = Guid.NewGuid();
PageManager manager = PageManager.GetManager();
PageData pageData =
null
;
PageNode pageNode =
null
;
// Get the parent node Id
if
(parentPageNodeId == Guid.Empty)
{
parentPageNodeId = SiteInitializer.CurrentFrontendRootNodeId;
}
PageNode parent = manager.GetPageNode(parentPageNodeId);
// Check whether exists
var initialPageNode = manager.GetPageNodes().Where(n => n.Id == pageId).SingleOrDefault();
if
(initialPageNode !=
null
)
{
return
;
}
// Create the page
pageData = manager.CreatePageData(pageDataId);
pageData.HtmlTitle = pageName;
pageData.Title = pageName;
pageData.Description = pageName;
pageData.Culture = Thread.CurrentThread.CurrentCulture.ToString();
pageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
pageNode = manager.CreatePage(parent, pageId, NodeType.Standard);
pageNode.Page = pageData;
pageNode.Name = pageName;
pageNode.Description = pageName;
pageNode.Title = pageName;
pageNode.UrlName = Regex.Replace(pageName.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
pageNode.ShowInNavigation =
true
;
pageNode.DateCreated = DateTime.UtcNow;
pageNode.LastModified = DateTime.UtcNow;
// Check whether home page
if
(isHomePage)
{
SystemManager.CurrentContext.CurrentSite.SetHomePage(pageId);
}
manager.SaveChanges();
// Publish
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageId,
typeof
(PageNode),
null
,
"Publish"
,
false
, bag);
}
The following code creates a page with the specified ID, Name and parent through the Native API. If needed, the page is set as home page.
First, you initialize the PageManager. You get the parent node. If the parent node is not specified, you use the FrontendRootNodeId. You check whether a page with the specified ID already exists. To do this, you get all page nodes using the GetPageNodes method and LINQ query. Then, if it does not exist, you create the page in a PageData object using the CreatePageData method. You create the page node using the CreatePage method. Then, you assign the page data to the Page property of the node. You set the properties of the page. It is recommended to set at least the following properties: Title, HtmlTitle and Description of the PageData object, and Name, Title, Description, UrlName, ShowInNavigation, DateCreated and LastModified of the PageNode object. To set the page as home page, you use the SetHomePage method. Then, you save the changes. Finally, to publish the page in live state, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.
Fluent API
public
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.UrlName = Regex.Replace(pageName.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
p.ShowInNavigation =
true
;
p.DateCreated = DateTime.UtcNow;
p.LastModified = DateTime.UtcNow;
p.Page.HtmlTitle = pageName;
p.Page.Title = pageName;
p.Page.Description = pageName;
p.Page.Culture = Thread.CurrentThread.CurrentCulture.ToString();
p.Page.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
})
.SaveChanges();
// Check whether home page
if
(isHomePage)
{
App.WorkWith().Page(pageId).SetAsHomePage().SaveChanges();
}
// Publish
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageId,
typeof
(PageNode),
null
,
"Publish"
,
false
, bag);
}
}
The following code creates a page with the specified ID, Name and parent through the Fluent API. If needed, the page is set as home page.
First, you get the parent node. If the parent node is not specified, you use the FrontendRootNodeId. You 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. Then, if it does not exist, you create the page node using the CreateNewStandardPage method of the facade. To create the page data, you use the Page property of the node. You set the properties of the page. It is recommended to set at least the following properties: Title, HtmlTitle and Description of the PageData object, and Name, Title, Description, UrlName, ShowInNavigation, DateCreated and LastModified of the PageNode object. To set the page as home page, you use the SetAsHomePage method of the singular page facade. Then, you save the changes. Finally, to publish the page in live state, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.