I have a helper class, which was working fine in EpiServer before we added an additional site to our instance. Now that we have added an additional site, the helper is throwing a null reference because the new Startpage
does not have any InsightsConfiguration.CoreStoriesConfig.CoreStories
.
I am trying to rewrite the code below to account for this null reference:
using EPiServer;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Web;
using System.Collections.Generic;
namespace Digital.Site.Helpers.Selections
{
public class DataLayerCoreStorySelectionFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
var loader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
var startPage = loader.Get<Models.Pages.StartPage>(SiteDefinition.Current.StartPage);
var coreStories = startPage.InsightsConfiguration.CoreStoriesConfig.CoreStories;
ISelectItem[] selectItems = new ISelectItem[coreStories.Count];
int i = 0;
foreach (var story in coreStories) {
selectItems[i++] = new SelectItem() { Text = story, Value = story};
}
return selectItems;
}
}
}
I have tried the following:
using EPiServer;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Web;
using System;
using System.Collections.Generic;
namespace Digital.Site.Helpers.Selections
{
public class DataLayerCoreStorySelectionFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
var loader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
var startPage = loader.Get<Models.Pages.StartPage>(SiteDefinition.Current.StartPage);
if (startPage.InsightsConfiguration.CoreStoriesConfig.CoreStories != null)
{
var coreStories = startPage.InsightsConfiguration.CoreStoriesConfig.CoreStories;
ISelectItem[] selectItems = new ISelectItem[coreStories.Count];
int i = 0;
foreach (var story in coreStories)
{
selectItems[i++] = new SelectItem() { Text = story, Value = story };
}
return selectItems;
}
}
}
}
But this throws the following error that I am unsure how to get around:
'DataLayerCoreStorySelectionFactory.GetSelections(ExtendedMetadata)': not all code paths return a value