EPiServer – Trying to get Correct Start Page Reference Inside a Job

I think this is a common issue when you create a job in EPiServer, you try to get the start page from the code and unfortunately the ContentReference.StartPage does not have the correct nor valid reference set in the variable. This post is about two possible solutions that you can use to solve this problem.

epi_server_logo_detail

So, lets begin !!!!

First, we will add the start page code, which is only a place holder with some properties to exemplify our scenario.


namespace Data.Models.Page
{
    [ContentType(GUID = "98448299-51bd-45a9-8d68-7dcdfee0a030")]
    public class StartPage : PageData
    {
        [Display(GroupName = SystemTabNames.Content, Order = 100)]
        public virtual string BrowserTitle { get; set; }

        [Required]
        [Display(GroupName = SystemTabNames.Content, Order = 200)]
        public virtual string PrimaryNavigationMenuLabel { get; set; }

        [Required]
        [Display(GroupName = SystemTabNames.Content, Order = 300)]
        public virtual string PrimaryNavigationMenuCloseLabel { get; set; }
    }
}

 

Now, we will add the code for the job we are going to run and will explain the code after it


namespace Business.Jobs
{
    [ScheduledPlugIn(DisplayName = "Published Articles")]
    public class PublishedArticles : ScheduledJobBase
    {
        private const string DemoSiteDefinitionName = "demo";
        private readonly Injected<IContentRepository> ContentRepository;
        private bool _stopSignaled;

        public PublishedArticles()
        {
            IsStoppable = true;
        }

        public override void Stop()
        {
            _stopSignaled = true;
        }

        public override string Execute()
        {
            //Call OnStatusChanged to periodically notify progress of job for manually started jobs
            OnStatusChanged($"Starting execution of {this.GetType()}");

            try
            {
                var settings = GetStartPage();

                if (settings == null)
                {
                    return "Setting page could not be found in this context.";
                }

                settings = GetStartPage(DemoSiteDefinitionName);

                if (settings == null)
                {
                    return "Setting page could not be found in this context.";
                }

                return "Ok";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public StartPage GetStartPage()
        {
            return ContentRepository.Service.Get(ContentReference.StartPage);
        }

        public StartPage GetStartPage(string siteDefinitionName)
        {
            var siteDefinitionRepository = ServiceLocator.Current.GetInstance();
            var siteDefinitions = siteDefinitionRepository.List().ToList();
            StartPage settingsPage = null;

            foreach (var siteDefinition in siteDefinitions)
            {
                if (string.Equals(siteDefinition.Name, siteDefinitionName, StringComparison.CurrentCultureIgnoreCase))
                {
                    settingsPage = ContentRepository.Service.Get(siteDefinition.StartPage);
                }
            }

            return settingsPage;
        }
    }
}

If we execute the job without any modification, it will raise an exception due to an invalid reference in the ContentReference.StartPage property.

The first possible solution, is to use the second method to get the start page which requires the name of the site that you want to get the start page as a parameter. The method will iterate over all site definitions and try to find the site using the parameter and then if the site is found, it will return the correct start page reference of the current site, This will allow to get the correct Content Reference Start Page but unfortunately requires a constant string value which represents the site name that if is changed it will require a compilation.

The second possible solution, is to add a wildcard to the list of host names of the site you want to get the start page content reference. This will allow the job to get the correct reference without requiring any other modification. You can modify this in the admin cms interface

And that is all. With any of these two possible solutions you can address this problem. Feel free to ask if you have doubts. I hope it will help someone and as always keep learning !!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s