Update layout data source references when copying a Sitecore item

Creating a page with faulty data source references

Every once in a while a content editor walks up to me to tell me that a new page does not show the content as expected. When a content editor copies an item to create a new page this usually means the layout references the data sources of the original. When you copy a page, the page is copied together with all underlying item, but the copied item still references the data sources as defined in the source item.

Correcting the data source references when copying a page

Fortunately there is an easy solution to this problem. Jan Hebnes mentions a LayoutDataSourceReferenceUpdater in a blog post some years ago (http://www.sitecore.net/sv-se/learn/blogs/best-practice-blogs/jan-hebnes/posts/2012/09/unsharing-the-layout-field-in-sitecore.aspx).
This piece of code is registered as an item:saved event. When you copy an item all references in the layout field that point to a data source that is a descendant of the item copied are updated to point to the equivalent item under the copied item. I have modified Jan’s gist (https://gist.github.com/janhebnes/3550920) to also support the final layout field.

If you have content editors that sometimes work in the content editor, this might save them (and yourself) a lot of work.

Check out the modified source here: https://github.com/martywoods/Sc.Modules.Events.LayoutDataSourceReferenceUpdater
Available on NuGet: http://www.nuget.org/packages/Sc.Modules.Events.LayoutDataSourceReferenceUpdater/

3 comments

  1. Hey Martijn,

    I noticed a very strange bug (which might be a bug in sitecore) using the datasource reference updater code.

    On some items that I duplicate the layoutfield is filled, but the ‘InnerField.HasValue’ property on that LayoutField is false.

    In the following post on stackoverflow, it is suggested that this can be caused by layout fields filled with only the standard value:
    http://stackoverflow.com/questions/35647588/sitecores-field-hasvalue-returning-false-even-when-there-is-a-value
    But that is not the case for me… all items are changed by content managers, so they have a LayoutField that is filled by users.
    That’s why I think this might be a sitecore bug.

    Turns out that InnerField.GetValue(true, true) != null is a more solid check than InnerField.HasValue, because the bools are allowStandardValue and allowDefaultValue.

    So I changed this line of code in the UpdateItemFields function:
    if (layoutField.InnerField.HasValue && itemBeingCopied.Languages.Any())
    to
    if (layoutField.InnerField.GetValue(true, true) != null && itemBeingCopied.Languages.Any())

    And I changed this line of code in the UpdateItemLayouts function:
    if (!layoutField.InnerField.HasValue)
    to
    if (layoutField.InnerField.GetValue(true, true) == null)

    That fixed the issue.

    Cheers,
    Nico

Leave a Reply to Nico Lubbers Cancel reply

Your email address will not be published. Required fields are marked *