Is it possible to set the page name programmatically in Episerver 11 ?

Vote:
 

Is it possible to set the page name programmatically in Episerver 11, and hide the page name input box on page creation?

And if possible, how can this be done? Is there an event hook which can be used?

#220877
Apr 07, 2020 9:55
Vote:
 

I haven't never tried it, but I think you can set the name inside your SetDefaultValue method (of your content type). If the name is not empty then the box will not probably not show 

#220879
Apr 07, 2020 10:34
Vote:
 

Hi,

Have you tried this-

PageData myPage = contentRepository.GetDefault<PageData>(parent, contentTypeRepository.Load("Standard page").ID);
myPage.PageName = "My new page";
contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

https://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Content/Creating-a-page-programmatically/

For disabling the properties you can hide them based on the roles.

And you can catch the create event pipeline-

[ModuleDependency(typeof(InitializationModule))]
public class MyCustomInitModule : IInitializableModule
{
    private IContentEvents _contentEvents;

    public void Initialize(InitializationEngine context)
    {
        if (_contentEvents == null)
        {
            _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        }

        _contentEvents.CreatingContent+= ContentEvents_CreatingContent;
    }

    private void ContentEvents_CreatingContent(object sender, ContentEventArgs e)
    {
        var articlePage = e.Content as ArticlePage;
        if (articlePage != null)
        {
            // TODO: ...
        }
    }

    public void Uninitialize(InitializationEngine context)
    {
        if (_contentEvents == null)
        {
            _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        }

        _contentEvents.CreatingContent -= ContentEvents_CreatingContent;
    }
}

FYI - You can use page name code in this initialization as well.

#220913
Edited, Apr 08, 2020 7:10
Vote:
 

Hi guys,

Thanks for your help. Eventually I used the event pipepeline:

private void ContentEvents_CreatingContent(object sender, ContentEventArgs e)
{           
   if (e.Content is DataPortalUpdatePage dataPortalUpdatePage)
   {
       dataPortalUpdatePage.Name = "Update " + 
                  DateTime.Now.Date.ToShortDateString();
   }

}

Is does show the Name input box, and the user input is replaced by the value in the code above, which is perhaps less than desirable. But the user can change it later.

#220914
Apr 08, 2020 7:20
Sanjay Kumar - Apr 08, 2020 8:01
Hi Guys, If you wish to give the page name for all page types such as ArticlePage, StadardPage, etc. then use PageData in the above event pipeline instead of one-page type (e.g. DataPortalUpdatePage).
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.