To duplicate an existing page within Sitefinity CMS using the native API, perform the following:
public
void
DuplicatePageNative(PageNode pageToCopy,
string
pageName)
{
var manager = PageManager.GetManager();
var newPagenode = manager.CreatePageNode();
var pageDataId = Guid.NewGuid();
PageData pageData =
null
;
PageNode pageNode =
null
;
PageNode parent = manager.GetPageNode(SiteInitializer.CurrentFrontendRootNodeId);
// Create the page data
pageData = manager.CreatePageData(pageDataId);
manager.CopyPageData(pageToCopy.Page, pageData);
pageNode = manager.CreatePage(parent, pageDataId, NodeType.Standard);
// set page node properties
pageNode.Page = pageData;
pageNode.Name = pageName;
pageNode.ShowInNavigation =
true
;
pageNode.UrlName = Regex.Replace(pageName.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
pageNode.Title = pageName;
// set page data properties
pageData.Title = pageName;
pageData.Description = pageName;
pageData.DateCreated = DateTime.UtcNow;
pageData.LastModified = DateTime.UtcNow;
manager.SaveChanges();
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageNode.Id,
typeof
(PageNode),
null
,
"Publish"
,
false
, bag);
}
To duplicate an existing page within Sitefinity CMS using the fluent API, perform the following:
public
void
DuplicatePageFluent(PageNode pageToCopy,
string
pageName)
{
var myDuplicatedPage = App.WorkWith().Page(pageToCopy.Id).Duplicate().Do(pageNode =>
{
pageNode.Name = pageName;
pageNode.ShowInNavigation =
true
;
pageNode.UrlName = Regex.Replace(pageName.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
pageNode.Title = pageName;
if
(pageNode.Page !=
null
)
{
var pageData = pageNode.Page;
pageData.Title = pageName;
pageData.Description = pageName;
pageData.DateCreated = DateTime.UtcNow;
pageData.LastModified = DateTime.UtcNow;
}
}).SaveAndContinue().Get();
// Publish
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(PageNode).FullName);
WorkflowManager.MessageWorkflow(myDuplicatedPage.Id,
typeof
(PageNode),
null
,
"Publish"
,
false
, bag);
}