Try our conversational search powered by Generative AI!

MigrationStep: accessing ContentReference returns null although the value is set

Vote:
 
Hi, we are having a strange problem as we tried to move on to MigrationSteps instead of manual deployment steps. In one page model we have a ContentReference required field which we would like to use as an initial value to set a new ContentArea property. The problem is, when this MigrationStep runs, the ContentReference property returns null, although the value is there in CMS (ID, language is the same). The value is also there when we run the controller for that content. The most interesting part is that if we manually run this MigrationStep from a context of a controller for instance, the ContentReference property returns the actual non-null value correctly the code succeeds.
public class MyMigrationStep : MigrationStep
{
    private readonly Injected _contentRepository;
    private readonly Injected _contentTypeRepository;
    private readonly Injected _contentModelUsage;

    public override void AddChanges()
    {
        var contentType = _contentTypeRepository.Service.Load();
        var contentReferences = _contentModelUsage.Service.ListContentOfContentType(contentType)
            .Select(contentUsage => contentUsage.ContentLink);

        foreach (var contentReference in contentReferences)
        {
            var model = _contentRepository.Service.Get(contentReference);

            if (model.ContentReferenceToBeCopied != null) // PROBLEM: this is null when running as a MigrationStep
            {
                var clone = (MyPageModel)model.CreateWritableClone();

                if (clone.ContentAreaToCopyTo == null)
                {
                    clone.ContentAreaToCopyTo = new ContentArea();
                }

                if (clone.ContentAreaToCopyTo.IsEmpty)
                {
                    clone.ContentAreaToCopyTo.Items.Add(new ContentAreaItem
                    {
                        ContentLink = clone.ContentReferenceToBeCopied
                    });

                    _contentRepository.Service.Publish(clone);
                }
            }
        }
    }
}

[ContentType(DisplayName = "MyPageModel", GUID = "ab83f1e2-2619-43c1-90a4-42e2fd35618b", Description = "")]
public class MyPageModel : PageTypeBaseModel
{
    [Display(
        Name = "ContentReferenceToBeCopied",
        GroupName = SystemTabNames.Content,
        Order = 100)]
    [Required]
    [UIHint(UIHint.CatalogEntry)]
    [AllowedTypes(typeof(MyVariationContent))]
    public virtual ContentReference ContentReferenceToBeCopied { get; set; }

    [Display(
        Name = "ContentAreaToCopyTo",
        Description = "",
        GroupName = SystemTabNames.Content,
        Order = 200)]
    [Required]
    [UIHint(UIHint.CatalogEntry)]
    [AllowedTypes(typeof(MyVariationContent))]
    public virtual ContentArea ContentAreaToCopyTo { get; set; }
}
But running the same manually from a controller works:
new MyMigrationStep().AddChanges()
Any ideas are welcome! Using EPiServer.Framework 10.10.4.
#196592
Edited, Sep 05, 2018 11:43
Vote:
 

I have not looked at the code in detail but my guess is that the migration step is run before the Model synchronization (that is when there is dynamically created proxy classes are created that "links" together the typed properties on your model class with the backing PropertyDataCollection).

If that is the case then you could try to access the data the "old" way as:

(model.Property["ContentReferenceToBeCopied"] as PropertyContentReference).ContentLink

#196682
Edited, Sep 07, 2018 15:42
Vote:
 
Thank you for your answer, in the Property collection it is there, so I can use that.
#196771
Sep 11, 2018 13:28
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.