Try our conversational search powered by Generative AI!

Copied page with the same name

Vote:
 

I was wondering if there is any way to append a text at the end of the copied page name to avoid confusion, so once user copies a page with name "Test" , the dupicated page has a name as "Test-Copy" .

#75565
Sep 30, 2013 12:56
Vote:
 

Andreas Nicolaisen at EPiNova created a blog post about a similar function:

http://www.epinova.no/blog/andreas-nicolaisen/dates/2011/9/create-your-own-copy-page-event/

I'm not certain if you can put it on the CreatedPage event or if it really needs to be on the PublishedPage since you might copy an unpublished Page.

The key is that e.Page should be null.

#75567
Sep 30, 2013 13:27
Vote:
 

Thank Alf. I tried this but it would not work. The problem is that the name does not change in treeview before the user publishes the page. Any solution for this?

#75574
Sep 30, 2013 15:31
Vote:
 

As I mentioned, I'm not sure whether you can change this to hookup to the CreatedPage event instead.

Could you change and see if this would add the name on create instead of publish?

#75575
Sep 30, 2013 15:34
Vote:
 

Or CreatingPage event instead?

#75577
Sep 30, 2013 15:41
Vote:
 

Yeah might even be better and then I guess you don't need to save the page separately.

Unfortunately I'm not able to try this out myself at the moment so all I can give are hints.

#75579
Sep 30, 2013 15:44
Vote:
 

Thank you both. I have actually used CreatedPage. The problem with using CreatingPage is that the Page property in PageEventArgs (CopyPageEventArgs) passed to CreatingPage is  null so is PageLink. There is a SourceLink property which has the link to the page that is being copied but I would have to do the copying myself and it might get messy.

#75582
Sep 30, 2013 16:26
Vote:
 

HI Reza,

would it be possible to add a snippet of how you did this... because if you hook into the createdPage event, how did you check if this was a new page or a copied page of an existing page..

#131682
Aug 03, 2015 11:29
Vote:
 

This if what i have for now

[InitializableModule]
    public class DataFactorySubscriber : IInitializableModule
    {
        public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            context.InitComplete += InitComplete;
        }

        private static void InitComplete(object sender, EventArgs e)
        {
            DataFactory.Instance.CreatedContent += CreatedContent;            
        }

        private static void CreatedContent(object sender, ContentEventArgs e)
        {           
            var content = e.Content;

            if (content != null)
            {
                CreateCopyPage(e.ContentLink);
            }        
        }      
       
        private static void CreateCopyPage(ContentReference contentLink)
        {
            var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
            //check if exists
            bool alreadyExists = repo.Get<PageData>(contentLink).GetSiblings().Where(p => p.Name == repo.Get<PageData>(contentLink).Name).Any();

            if (!alreadyExists)
            {
                return;
            }
            
            var variant = repo.Get<PageData>(contentLink);
            var clone = variant.CreateWritableClone();
            clone.PageName = variant.Name + " - Copy";            
            clone.URLSegment = "";
            // Does this page have a simple address?
            //clone.SetPropertyValue(p => p.URLSegment, "");
            
            DataFactory.Instance.Save(clone, SaveAction.Save, AccessLevel.NoAccess);
        }

        public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            context.InitComplete -= InitComplete;
            DataFactory.Instance.CreatedContent -= CreatedContent;            
        }

        public void Preload(string[] parameters)
        {
        }
    }

The problem is two fold: It was first complaining that the URL in Name is already in use. So i set the URLSegment to "" .

But still my copied page doesnt get the new pageName with  -Copy behind it. And this fails completely if there the page being copied containt contentassetfolders (blocks for example).

What would be a correct way to rename a copied page (with or without assets) from code?

#131692
Edited, Aug 03, 2015 13:43
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.