Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
The error is happening in EPiServer.Cms.Shell.UI.ObjectEditing.InternalMetadata.LinkModel.ToServerModel(object clientModel). If there isn't a LinkItem, clientModel is null, and therefore the line that reads if (linkModel.Attributes != null) will throw a System.NullReferenceException because linkModel is null. There needs to be a null-check after LinkModel linkModel = (LinkModel) clientModel;
Code in case the image doesn't work:
public virtual object ToServerModel(object clientModel)
{
LinkModel linkModel = (LinkModel) clientModel;
LinkItem serverModel = new LinkItem();
if (linkModel.Attributes != null) // <!!-- This is where it throws the System.NullReferenceException
{
foreach (KeyValuePair<string, string> attribute in linkModel.Attributes)
serverModel.Attributes.Add(attribute.Key, attribute.Value);
}
serverModel.Text = linkModel.Text;
serverModel.Title = linkModel.Title;
LinkItem linkItem = serverModel;
...
We have a block as a property for the page hero. Inside that HeroBlock, we have a LinkItem for a CTA button. If we don't have any LinkItems (call-to-actions), we can't save updates to the other properties like Title and Description when using on-page editing. The all-properties view works; however, we're prioritizing the on-page editing experience.
The error received is exactly "Could not save property, and it has been reverted. Please try again. Object reference not set to an instance of an object". When I check the console, there's an error that has the following stack trace and headers:
I have identified the problem as an attempt to deserialize a null value from the LinkItem object. Here is the Ajax payload being sent:
propertyValue: {"indexInContentAreas":false,"image":null,"headerTitle":"Site Search","description":"<p>This is a description</p>","callToAction":null}
If a LinkItem is added to the CallToAction property everything works. Here is an Ajax payload with a LinkItem in the CallToAction property that allows me to update the Title or Description:
propertyValue: {"indexInContentAreas":false,"image":null,"headerTitle":"Site Search","description":"<p>This is a description!!</p>","callToAction":{"text":"Test Link","href":"http://google.com","target":null,"title":"Test Link","publicUrl":null,"typeIdentifier":""}}
Finally, here is how the block is set up, in case that sparks an idea.
[ContentType( GroupName = GroupNames.Content, DisplayName = "Hero", GUID = "39483886-ba15-45ca-a868-12cb2e4971a8", Description = "Hero component for Carousel Blocks, Section Hero, and Banner" )] public class HeroBlock : BaseBlock, IHeroBlock, IPageContentBlock { [Display( Name = "Title", Order = 10)] [Required] [CultureSpecific] public virtual string HeaderTitle { get; set; } [Display( Name = "Image", GroupName = SystemTabNames.Content, Order = 20 )] [CultureSpecific] [AllowedTypes(new[] { typeof(ImageMediaData) })] [DefaultDragAndDropTarget] [UIHint(UIHint.Image)] [OptionBarItem] [FullRefresh] public virtual ContentReference Image { get; set; } [Display( GroupName = SystemTabNames.Content, Order = 30)] [CultureSpecific] [Searchable] public virtual XhtmlString Description { get; set; } [Display( GroupName = SystemTabNames.Content, Name = "Call To Action ", Order = 40)] [CultureSpecific] public virtual LinkItem CallToAction { get; set; } }
To get it to work, I'm trying to find where it gets deserialized to add a null check, but I have no idea where that could be. I've also seen this same issue in other unsolved forum threads going back to 2015.
https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2017/10/json-serialization-error-when-creating-a-link-item/
https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2015/11/unable-to-publish-linkitemcollection-properties/