Try our conversational search powered by Generative AI!

Update Start page content based on content of the latest page of another type

Vote:
 

Hi All,

I want to update start page content based on the creating the new instance of another page.

I have two PageTypes StartPage and ArticlePage.So basically on addition of new Article Page in my application I want the content of that page to be copied to StartPage.

ArticlePage and StartPage is having Same Properties .

Does anyone having idea how it can be achieved?

Basically I want the latest Article page to be startPage.As in EpiServer we need to fix the StartPage of the application that's why I want to copy the content of the latest Article page?

#145739
Mar 10, 2016 17:09
Vote:
 

It's tricky to give a good suggestion without knowing what you expect it to look like, and what your prerequisites are.

For example, are you using WebForms or MVC?

Second, do you want the ArticlePage to be copied to the start page in a way so that if the editor edits the start page, the properties will have the same value of your newly created standard page? Or is it enough that the visitor sees the information from the article page when visiting the start page?

Do you need to copy content?
If it's enough with only the visitor seeing the article page information I would suggest that you simply get the content based on the most recently created article page and displays it. The content of your article page doesn't have to be copied to the start page's properties if you only want it displayed.

Copy content
Secondly if you actually need the content from your article page to be copied to your start page's properties I would suggest that you look into Episerver's content Events and modify your start page.

When modifying the start page, copy the content from the start page or use the Shortcut functionality in Episerver (see example on shortcuts but instead use PageShortcutType.FetchData)

For example to modify start page

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class PublishEventInitializationModule : IInitializableModule
{
  public void Initialize(InitializationEngine context)
  {
    var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();

    contentEvents.PublishingContent += ContentEventsPublishingContent;
  }
  
  public void Preload(string[] parameters) { }

  public void Uninitialize(InitializationEngine context)
  {
    var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
    contentEvents.PublishingContent -= contentEvents_PublishingContent;
  }

 private void ContentEventsPublishingContent(object sender, EPiServer.ContentEventArgs e)
 {
  var articlePage = e.Content as ArticlePage;
  if(articlePage == null)
    return;

  var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
  var startPage = contentRepository.Get<StartPage>(ContentReference.StartPage);

  var clone = startPage.CreateWritableClone() as StartPage;
  if(clone == null)
    return;

  // Either copy content from articlePage, or set the Shortcut functionality

  contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);
 }
}

I would REALLY recommend that you don't change your start page unless you really need to. If you only want to display the content from the latest article when a user is visiting the start page, just locate and load the latest article page in your Controller or .aspx backend and output it.

#145746
Edited, Mar 10, 2016 23:48
Vote:
 

1. Is it enough to display the information of the last article page on start page? In that case you should probably implement logic in startpage controller to get the last published article page...and probably cache it...

2. React to published event of article page and either update startpage programmatically with all properties or maybe just use the fetch data from function and set that to link to the last article page that was published.

3. Let editor set fetch data from manually on start page after publishing new article = no coding necessary...

SEO might be worth considering...google doesn't like duplicate content... 

#145747
Mar 10, 2016 23:57
Vote:
 

Thank you @alifNilson and Daniel Vasaka for the reply.

Alif:

I am using MVC and I want to display the latest Article page as my start page to the visitor.As you suggested we should not change startpage data  unless it's very necessary.

I agree with you on that,Can suggest me some sample code which displays latest page as startpage to visitor.That will be very much helpful to me.

#145748
Mar 11, 2016 6:08
Vote:
 

@daniel and @Alif :I would prefer not to change the startpage data and load latest article page .

My target is to show the latest Article page to viewer not startPage.

#145749
Mar 11, 2016 6:22
Vote:
 

Depends on how you have your article pages?

  • do you want any article page, doesn't matter where it is created, to be displayed?
  • do you have a specific node in your tree structure where you have article pages, and only article pages created there to be displayed?

Daniel's suggestion where the editor selects the article page(s) to be displayed is good. Requires a little more manual work for the editor but it wouldn't surprise me that the editor sometimes doesn't agree with the article page from the automatic selection. Then it's easy to change.

for one page, use a page reference property. For more use a contentarea 

step two could be to automatically set this page selection using the code above.

#145752
Mar 11, 2016 8:17
Vote:
 

@alif any article page, doesn't matter where it is created, to be displayed is my requirement.

can you guide me bit more how the above code can be reused.

StartPage is my default page,I can't change it because all the common configurable properties like properties for header,Footer,LeftSidebaar and RightSideBar resides there.

I have kept those properties in startPage because i want to give access Editor to change those properties from startPage itsef not from other pages.

So please suggest me which will be best approach?

#145761
Mar 11, 2016 9:26
Vote:
 

I don't see why you can't add a property for this on your start page? You will only change your new property and whatever the editor selected on the footer, header etc won't change.
Setting the latest article would have much better performance than looking for the latest article in your entire page tree.

If you want to find all pages of a specific type, I would suggest FindPagesWithCriteria. I've got some demo example of this at https://github.com/alfnilsson/ascend2015/blob/master/Business/_StandardPageRepository/5%20CachedFindPagesWithCriterion.cs (if you want a more secure mechanism with Locking, use https://github.com/alfnilsson/ascend2015/blob/master/Business/_StandardPageRepository/6%20LockingCachedFindPagesWithCriterion.cs).
Note that this is a demo example, you should remove the Thread.Sleep and instead of caching the page, cache the page.PageLink instead.

Once you get your PageDataCollection from FindPagesWithCriteria, sort and select the first one with either LINQ or Jay's example on http://world.episerver.com/Forum/Developer-forum/Developer-to-developer/Thread-Container/2007/4/FindPagesWithCriteria---is-there-any-way-to-limit-the-number-of-pages-returned/

Again, I would really recommend that the latest article is set as a property on the start page instead as it would mean less complexity in your code and also perform better.

Just a side note: Having settings and global content on a separate content (settings page/settings block) is a better approach so that the Start Page only contains things that actually are related to the start page. But I can understand that you can't change your setup at this stage.

#145764
Mar 11, 2016 9:39
Vote:
 

@Alif I am trying to use your first sample code where i want to copy the content of newly created article page to start page i am trying like this but that does not seems to be working.

Please guide me if i am doing anything wrong.

 private void ContentEventsPublishingContent(object sender, EPiServer.ContentEventArgs e)
        {
            var articlePage = e.Content as ArticlePage;
            if (articlePage == null)
                return;

            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var startPage = contentRepository.Get<StartPage>(ContentReference.StartPage);

            var clone = startPage.CreateWritableClone() as StartPage;
            if (clone == null)
                return;

//THese are the two properties I want to copy to StartPage
            clone.MainBody = articlePage.MainBody;
            clone.PageHeaderText = articlePage.PageHeaderText;

           
        }

#145767
Mar 11, 2016 9:50
Vote:
 

@Alif I am trying to use your first sample code where i want to copy the content of newly created article page to start page i am trying like this but that does not seems to be working.

Please guide me if i am doing anything wrong.

 private void ContentEventsPublishingContent(object sender, EPiServer.ContentEventArgs e)
        {
            var articlePage = e.Content as ArticlePage;
            if (articlePage == null)
                return;

            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var startPage = contentRepository.Get<StartPage>(ContentReference.StartPage);

            var clone = startPage.CreateWritableClone() as StartPage;
            if (clone == null)
                return;

//THese are the two properties I want to copy to StartPage
            clone.MainBody = articlePage.MainBody;
            clone.PageHeaderText = articlePage.PageHeaderText;

           
        }

#145768
Mar 11, 2016 9:51
Vote:
 

@Alif I am trying to use your first sample code where i want to copy the content of newly created article page to start page i am trying like this but that does not seems to be working.

Please guide me if i am doing anything wrong.

 private void ContentEventsPublishingContent(object sender, EPiServer.ContentEventArgs e)
        {
            var articlePage = e.Content as ArticlePage;
            if (articlePage == null)
                return;

            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var startPage = contentRepository.Get<StartPage>(ContentReference.StartPage);

            var clone = startPage.CreateWritableClone() as StartPage;
            if (clone == null)
                return;

//THese are the two properties I want to copy to StartPage
            clone.MainBody = articlePage.MainBody;
            clone.PageHeaderText = articlePage.PageHeaderText;

           
        }

#145769
Mar 11, 2016 9:52
Vote:
 

Forget about copying the content, it's better to fetch the information instead.

On your start page, create a public virtual PageReference LatestArticle{get;set;}

 private void ContentEventsPublishingContent(object sender, EPiServer.ContentEventArgs e)
        {
            var articlePage = e.Content as ArticlePage;
            if (articlePage == null)
                return;

            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var startPage = contentRepository.Get<StartPage>(ContentReference.StartPage);

            var clone = startPage.CreateWritableClone() as StartPage;
            if (clone == null)
                return;
            clone.LatestArticle = articlePage.PageLink;

            contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);

        }


In your Controller or the backend of your .aspx that belongs to your start page, get the content based on currentPage.LatestArticle using the normal Episerver API for content http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Content/icontentrepository-and-datafactory/

#145772
Edited, Mar 11, 2016 9:56
Vote:
 

@alf Thank you very much sir,It worked for me,I really appreciate your help.

On update also it's firing I want it to restrict it only for creation of new page and updating it.

#145779
Edited, Mar 11, 2016 10:37
Vote:
 

You're correct, maybe the CreatedContent event would work better, it should be very similar to Published but only be fired when the content is created.

Here is a list of the different events.

http://world.episerver.com/documentation/class-library/?documentId=cms/7/306eae4b-2ba2-dd1e-c114-bccb0d3d2968

#145780
Mar 11, 2016 10:48
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.