Duplicate Page Group
In Sitefinity 3.x I added a custom control to duplicate pages groups. In Sitefinity 4.4 this functionality is still not available by default. How can I add an Item to the page group context menu on the page list screen so that I can write this functionality again?
bump
Are there any examples on adding items to the context menus on the Page List in the admin?
Hello,
we are in need of the same function ( duplicate page groups ) , did anyone find a solution or has some sample code to manage duplication , also not adding in the admin?
Thank you very much !!!
Not sure if this is what you need but this is what i wrote up to duplicate page groups and the structure of pages beneath them, given a selected pagenode. maybe it will help. there is nothing existing in SF to do it, that i know of.
you just have to call CopyPageGroup() with the right values and it does the rest. checks for duplicate titles and urlnames.
public
PageNode CopyPageGroup(Guid NodeToCopyFrom,
string
NewRoot_Title,
string
NewRoot_HTMLTitle,
string
NewRoot_Description)
PageManager manager = PageManager.GetManager();
PageNode newNode =
null
;
//get main root node
Guid parentPageNodeId = SiteInitializer.FrontendRootNodeId;
PageNode rootNode = manager.GetPageNode(parentPageNodeId);
if
(rootNode !=
null
)
//build list of all child nodes to be duplicated
List<PageNode> existingNodeList =
new
List<PageNode>();
_FindChildNodes(rootNode, existingNodeList);
int
nameCounter = 1;
string
tempName =
""
;
//make sure the new base page node doesnt already exist
if
(_CheckForExistingPage(NewRoot_Title, rootNode.Nodes.ToList()) !=
null
)
tempName = NewRoot_Title +
"_"
+ nameCounter;
while
(_CheckForExistingPage(tempName, rootNode.Nodes.ToList()) !=
null
)
nameCounter++;
tempName = NewRoot_Title +
"_"
+ nameCounter;
if
(tempName ==
""
)
newNode = _DuplicatePageHierarchy(NodeToCopyFrom, NewRoot_Title, NewRoot_HTMLTitle, NewRoot_Description, existingNodeList, rootNode);
else
newNode = _DuplicatePageHierarchy(NodeToCopyFrom, tempName, NewRoot_HTMLTitle, NewRoot_Description, existingNodeList, rootNode);
return
newNode;
private
PageNode _CheckForExistingPage(
string
NewRoot_Title, List<PageNode> PageNodes)
//check if partner page already exists
PageNode pageNode =
null
;
foreach
(PageNode pn
in
PageNodes)
if
(pn.Title.ToLower() == NewRoot_Title.ToLower())
pageNode = pn;
break
;
return
pageNode;
private
void
_FindChildNodes(PageNode checkNode, List<PageNode> nodeList)
foreach
(PageNode pn
in
checkNode.Nodes)
if
(pn.NodeType == NodeType.Group || pn.Page !=
null
)
nodeList.Add(pn);
_FindChildNodes(pn, nodeList);
private
PageNode _DuplicatePageHierarchy(Guid NodeToCopyFrom,
string
NewRoot_Title,
string
NewRoot_HTMLTitle,
string
NewRoot_Description, List<PageNode> existingNodeList, PageNode rootNode)
if
(NodeToCopyFrom == Guid.Empty || String.IsNullOrEmpty(NewRoot_Title))
return
null
;
else
PageManager manager = PageManager.GetManager();
var pageDataId = Guid.NewGuid();
PageNode selectedNode = _FindSelectedNode(NodeToCopyFrom, existingNodeList);
PageData newPageData =
null
;
PageNode newPageNode =
null
;
if
(selectedNode.NodeType != NodeType.Group)
newPageData = manager.CreatePageData(pageDataId);
newPageData.Title = NewRoot_Title;
if
(String.IsNullOrEmpty(NewRoot_HTMLTitle))
newPageData.HtmlTitle = NewRoot_Title;
else
newPageData.HtmlTitle = NewRoot_HTMLTitle;
if
(NewRoot_Description ==
null
)
newPageData.Description = selectedNode.Description;
else
newPageData.Description = NewRoot_Description;
newPageData.Template = selectedNode.Page.Template;
newPageData.Culture = Thread.CurrentThread.CurrentCulture.ToString();
newPageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
newPageNode = manager.CreatePage(rootNode, Guid.NewGuid(), selectedNode.NodeType);
newPageNode.Page = newPageData;
newPageNode.Name = NewRoot_Title;
newPageNode.Title = NewRoot_Title;
if
(NewRoot_Description ==
null
)
newPageNode.Description = selectedNode.Description;
else
newPageNode.Description = NewRoot_Description;
newPageNode.UrlName = Regex.Replace(NewRoot_Title.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
newPageNode.ShowInNavigation =
true
;
newPageNode.DateCreated = DateTime.UtcNow;
newPageNode.LastModified = DateTime.UtcNow;
//check for duplicate node urls
if
(PageManager.CheckNodeUrlForDuplicates(newPageNode))
//duplicates found
int
counter = 1;
while
(PageManager.CheckNodeUrlForDuplicates(newPageNode))
newPageNode.UrlName = Regex.Replace((NewRoot_Title +
"_"
+ counter).ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
counter++;
try
manager.SaveChanges();
// Publish
if
(newPageNode.NodeType == NodeType.Standard)
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(PageNode).FullName);
WorkflowManager.MessageWorkflow(newPageNode.Id,
typeof
(PageNode),
null
,
"Publish"
,
false
, bag);
_BuildChildNodes(newPageNode, selectedNode.Nodes.ToList());
catch
newPageNode =
null
;
return
newPageNode;
private
PageNode _FindSelectedNode(Guid NodeToCopyFrom, List<PageNode> existingNodeList)
//find the selected node
PageNode selectedNode =
null
;
foreach
(PageNode pn
in
existingNodeList)
if
(pn.Id == NodeToCopyFrom)
selectedNode = pn;
return
selectedNode;
private
void
_BuildChildNodes(PageNode rootNode, List<PageNode> existingNodeList)
PageManager manager = PageManager.GetManager();
foreach
(PageNode pn
in
existingNodeList)
PageData pageData =
null
;
PageNode pageNode =
null
;
if
(pn.NodeType != NodeType.Group)
pageData = manager.CreatePageData(rootNode.Id);
pageData.HtmlTitle = pn.Title;
pageData.Title = pn.Title;
pageData.Description = pn.Description;
pageData.Template = pn.Page.Template;
pageData.Culture = Thread.CurrentThread.CurrentCulture.ToString();
pageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
pageNode = manager.CreatePage(rootNode, Guid.NewGuid(), pn.NodeType);
pageNode.Page = pageData;
pageNode.Name = pn.Name;
pageNode.Description = pn.Description;
pageNode.Title = pn.Title;
pageNode.UrlName = Regex.Replace(pn.Title.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
pageNode.ShowInNavigation =
true
;
pageNode.DateCreated = DateTime.UtcNow;
pageNode.LastModified = DateTime.UtcNow;
pageNode.InheritsPermissions =
true
;
//check for duplicate node urls
if
(PageManager.CheckNodeUrlForDuplicates(pageNode))
//duplicates found
int
counter = 1;
while
(PageManager.CheckNodeUrlForDuplicates(pageNode))
pageNode.UrlName = Regex.Replace((pn.Title +
"_"
+ counter).ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
counter++;
try
manager.SaveChanges();
// Publish
if
(pageNode.NodeType == NodeType.Standard)
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageNode.Id,
typeof
(PageNode),
null
,
"Publish"
,
false
, bag);
_BuildChildNodes(pageNode, pn.Nodes.ToList());
catch
Thank you very much for your help , I will try it and let you know!!!