Try our conversational search powered by Generative AI!

Problem creating a new page in C#

Vote:
 

Hello all,

I'm getting an error when I try to add a new page into a ContentReference.

The page type (Article) has the following attribute:

[AllowedTypes(typeof(SpecialistPage))]
[Display(Name = "Auteurs", Description = "De auteurs van het artikel", Order = 40, GroupName = SystemTabNames.Content)]
public virtual IList Authors { get; set; }

I'm able to create a new article page, but when I try to add a page into the Authors ContentReference block with the following code:

var cr = ServiceLocator.Current.GetInstance();
var articleOverview = cr.GetChildren(EPiServer.Core.ContentReference.StartPage).FirstOrDefault();

var articlePage = ContentRepository.GetDefault(articleOverview.ContentLink);

if (authorPreference != null)
{
    foreach (var author in authorPreference)
    {
        var page = ContentRepository.Get(Guid.Parse(author));
        articlePage.Authors.Add(page.ContentLink);
    }
}

I get an exception "System.NullReferenceException: Object reference not set to an instance of an object."

I can see that the variable page is correctly created so the page.ContentLink exist, so don't know why I'm getting this error.

Thanks in advance.

#199104
Nov 15, 2018 13:02
Vote:
 

You would need to initiate Authors, for example by SetDefaultValues 

public override void SetDefaultValues(ContentType contentType)
{
base.SetDefaultValues(contentType);
  Authors = new List<ContentReference>();
}
#199105
Nov 15, 2018 13:32
Vote:
 

Hello Quan,

Thanks for you help. It works :)

Best,

#199137
Nov 16, 2018 11:11
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.