Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Publishing pages programatically

Vote:
 

Hello,

 

I'm trying to build a plugin that recursivly publishes all the latest version of pages below the current node in the EPiServer tree. It is intended to be run after our scheduled product import job after verifying that everything is correct. My solution is almost ready but I've noticed a big problem that I have not been able to solve yet.

 

A lot of the imported product pages are actually duplicates of each other, just located at different places in the structure. To solve this without having duplicated content in EPiServer and without loosing SEO score for duplicated content we use the "Fetch data from another page" feature in EPiServer, PageShortCutType == PageShortcutType.FetchData, together with rendering out link rel="canonical" on the duplicated pages.

 

Now to the actual problem. To recursivly publish all the pages I've been using this code:

 

 

private IContentRepository contentRepository;

private IEnumerable<ContentReference> CurrentDescendants
{
    get
    {
        return contentRepository.GetDescendents(CurrentContentLink);
    }
}

protected string CurrentLanguageBranch
{
    get
    {
        return contentRepository.Get<PageData>(CurrentContentLink).LanguageBranch;
    }
}

private IEnumerable<PageData> PagesAwaitingPublish
{
    get
    {
        var langSelector = new LanguageSelector(CurrentLanguageBranch);
        return CurrentDescendants.Select(page =>
            {
                PageData content;
                return !this.contentRepository.TryGet(page, langSelector, out content) ? null : content;
            }).Where(page => page != null && page.IsPendingPublish);
    }
}

protected void DoRecursivePublish(object sender, EventArgs args)
{
    PagesAwaitingPublish.ForEach(page => contentRepository.Save(page.CreateWritableClone(), (SaveAction.Publish | SaveAction.ForceCurrentVersion), AccessLevel.NoAccess));
}

    

Unfortunately what I noticed was that it erased all the PageShortcutType and PageShortcutLink properties on the newly published pages.

Now this might make sense after having called CreateWriteable clone, so my idea was to try an read the properties from the original page first and then set them back on the clone before publishing it. But even then, they seem to be blank...

 

But maybe that is because I'm just trying to retrieve data from the orginal non-fetch data page? Since the current page in the loop does have fetch data set when I'm trying to publish it?

But following the examples in this (pretty old) discussion: http://world.episerver.com/modules/forum/pages/thread.aspx?id=31471 has yielded no results...

I'm cluess now as to why the following if-case never happens:

// TODO: This fetchdata stuff does NOT work
var clone = page.CreateWritableClone();
if ((PageShortcutType)page.Property.Get("PageShortcutType").Value == PageShortcutType.FetchData)
{
    clone.LinkType = PageShortcutType.FetchData;
    clone.Property.Set("PageShortcutLink", clone.Property.Get("PageShortcutLink"));
}

    

Trying to read the property from the writeable clone instead of the page (as suggested in the linked thread) yields no different result either.

 

Any ideas? Is there a better way to publish pages programatically?

#80831
Feb 03, 2014 14:48
Vote:
 

Did you get the FetchData working? i have problem getting the FetchData working on episerver 7.19.2

#118713
Mar 12, 2015 11:55
Vote:
 

If I remember correctly my problem was that my recursive publishing was removing the FetchData property from the pages as I was publishing them.

This is no longer the case, I don't remember what fixed it though. We are also running EPiServer 7.19.2.

The only difference I can see in our current version of the recursive publishing code and what I posted here is that I no longer use "(SaveAction.Publish | SaveAction.ForceCurrentVersion)", but instead only "SaveAction.Publish".

Afaik, you are not supposed to use several flags on the DataFactory.Save command.

#118790
Mar 13, 2015 15:24
* 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.