Try our conversational search powered by Generative AI!

Importing content with non-unique ContentType names [Workaround]

Vote:
 

Hi,

I think i'm running into an issue with multiple contenttypes with the same name when importing content from another machine. Having multiple contenttypes with the same name probably is a bad practise, but seems to work without a problem except when importing content. I've done some debugging and found out that normally in an export only the PageTypeID (integer) and PageTypeName (string) is exported. If the option "Automatically export dependent contenttypes" is checked, also the data of the dependent contenttypes are exported.

On the import source system the names of the ContentTypes is different in comparison to the import destination. On the source i see "Homepage" and "Homepage(1)" as ContentTypes. These names are made unique by adding a number to the original pagename. On my destination machine i also see the same contenttype names, but the contenttypes are exchanged. So source "Homepage" is named "Homepage(1)" on the destination machine. It seems that during the import the property "PageTypeName" is used to create content instances.
The result is that pages are created with the wrong ContentType. The reason that the names are switched is probably due to the order of creating the ContentTypes on the source machine vs. the order of loading assemblies on the destination machine.

I created a custom InitializableModule to hook into the IDataImportEvents and catch the ContentImporting events. When the ContentTypes were also exported i see that the properties ContentTypes and ContentTypesMap are filled. By looking up the PageTypeName of items in the ContentTypes list and retrieving the ContentType in the destination repository by it's Guid, i'm able to switch the names present in the export-file to the names in the destination.

I did notice that some dependent ContentTypes were not automatically exported. I resolved this by selecting these missing ContentTypes for export.

I think the following 'flaws' are present (at least in EPiServer v9.12.3):
* When exporting ContentItems the export should contain the PageType GUID if these are set by code.
* The option "Automatically export dependent contenttypes" can miss some ContentTypes.

I'm interested if these can be resolved in another way than renaming the code-first classnames.

With the following code i'm able to workaround the issue:

Snippet

        private static void ImportEvents_ContentImporting(EPiServer.Enterprise.Transfer.ITransferContext transferContext, ContentImportingEventArgs e)
        {
            if (transferContext.ContentTypes != null && transferContext.ContentTypes.Any())
            {
                var pageTypeNameProperty = RetrievePageTypeNameProperty(e);
                if (pageTypeNameProperty == null)
                {
                    e.Cancel = true;
                } else
                {
                    var correctContentType = DetermineCorrectContentType(pageTypeNameProperty.Value, transferContext.ContentTypes);
                    if (correctContentType==null)
                    {                        
                        e.Cancel = true;
                    } else
                    {
                        if (!correctContentType.Name.Equals(pageTypeNameProperty.Value))
                        {
                            pageTypeNameProperty.Value = correctContentType.Name;
                        }
                    }
                }                
            }
        }
 
        private static RawProperty RetrievePageTypeNameProperty(ContentImportingEventArgs e)
        {
            return e.TransferContentData.RawContentData.Property.SingleOrDefault(p => p.Name.Equals(MetaDataProperties.PageTypeName));
        } 
 
        private static ContentType DetermineCorrectContentType(string pageTypeName, IList<>ContentType> contentTypes)
        {
            var ct = contentTypes.FirstOrDefault(a => a.Name.Equals(pageTypeName));
            if (ct != null)
            {
                ct = contentTypeRepository.Load(ct.GUID);
            }
            return ct;
        }
#174553
Jan 30, 2017 12:01
Vote:
 

This is interesting article, as we have currently in process of migrating 3 Episerver code bases into one. I was also wondering whether content import is using ContentType guids or just names, because we would like to rename some content types before migrating them to one code page. But if we rename the types, then export from old separate site to new migrated code base site would fail because page types would not match. 

Did you Sander come up with some other solutions related the problem you had? The idea of hooking to exporter events sounds attempting also to us.

#196002
Aug 15, 2018 15:57
* 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.