KennyG
Jan 25, 2019
  2547
(1 votes)

PropertyList Changes Not Detected, Adding vs Updating

So here is a little nuance, that took me some time to figure out. I was programmatically updating a PropertyList but it never seemed to actually update.

Let’s take the following propertylist as an example:

namespace PropertyListDemo.Models
{
    public class Character
    {
        [Display(Name = "Hero Name")]
        public string Name { get; set; }
        [Display(Name = "Team Affiliation")]
        public string Affiliation { get; set; }
        [Display(Name = "Secret Identity Name")]
        public string SecretIdentity { get; set; }
    }

    [PropertyDefinitionTypePlugIn]
    public class CharacterListProperty : PropertyListBase<Character>
    {
    }

    public class PropertyListBase<T> : PropertyList<T>
    {
        public PropertyListBase()
        {
            _objectSerializer = this._objectSerializerFactory.Service.GetSerializer("application/json");
        }
        private Injected<ObjectSerializerFactory> _objectSerializerFactory;

        private IObjectSerializer _objectSerializer;
        protected override T ParseItem(string value)
        {
            return _objectSerializer.Deserialize<T>(value);
        }

        public override PropertyData ParseToObject(string value)
        {
            ParseToSelf(value);
            return this;
        }
    }

    [EditorDescriptorRegistration(TargetType = typeof(IList<Character>))]
    public class CharacterCollectionEditorDescriptor : CollectionEditorDescriptor<Character>
    {
    }
}

Adding to the propertylist worked as expected:

            var standardPage = currentPage.CreateWritableClone() as StandardPage;

            if (standardPage?.Heroes != null && standardPage.Heroes.Any())
            {

                standardPage.Heroes.Add(new Character()
                {
                    Name = "Hulk",
                    Affiliation = "Avengers",
                    SecretIdentity = "Bruce Banner"
                });


                contentRepository.Save(standardPage, SaveAction.Publish, AccessLevel.NoAccess);
            }

However, if instead of adding I was trying to update the propertylist:

            var standardPage = currentPage.CreateWritableClone() as StandardPage;

            if (standardPage?.Heroes != null && standardPage.Heroes.Any())
            {
                var match = standardPage.Heroes.FirstOrDefault(x => x.Name == "Superman");
                if (match != null)
                {
                    match.Name = "Iron Man";
                    match.Affiliation = "Avengers";
                    match.SecretIdentity = "Tony Stark";
                }


                contentRepository.Save(standardPage, SaveAction.Publish, AccessLevel.NoAccess);
            }

It would act like it had saved but the data wouldn’t be any different.

After a lot of time Googling and debugging, I determined that there is a Boolean “IsModified” flag from the ContentData abstract class.

standardPage.Property["Heroes"].IsModified = true;

This needs to be true when saving the page. When adding to the propertylist it is true, but when updating it is false, so it has to be manually set. So, an update followed by an add would work but update by itself doesn’t work.

So, it’s a good idea to go ahead and set the flag yourself before saving the page.

            var standardPage = currentPage.CreateWritableClone() as StandardPage;

            if (standardPage?.Heroes != null && standardPage.Heroes.Any())
            {
                var match = standardPage.Heroes.FirstOrDefault(x => x.Name == "Superman");
                if (match != null)
                {
                    match.Name = "Iron Man";
                    match.Affiliation = "Avengers";
                    match.SecretIdentity = "Tony Stark";
                }

                standardPage.Property["Heroes"].IsModified = true;

                contentRepository.Save(standardPage, SaveAction.Publish, AccessLevel.NoAccess);
            }
Jan 25, 2019

Comments

Praful Jangid
Praful Jangid Mar 5, 2019 02:55 PM

Is this the case only applicable for PropertyList type? Or we need to set this IsModified for any kind of Episerver property type?

KennyG
KennyG Mar 5, 2019 10:05 PM

Hey Praful, I tried a quick test with a string and it updated just fine. I can't say it's all types but simple ones seem fine.

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024