Custom part properties missing in export Orchard 1.6
Asked Answered
J

2

6

I have a problem with the import export module in Orchard 1.6:
I want to export a custom type with a part that has a property in it. The export XML contains data from TitlePart, CommonPart, BodyPart and AutoroutePart, however the data from my own part is not there.

Is there anything I should do like implementing an interface or overriding something on my part so that it is contained within the export XML? What are (if any) the extension points of the export module? I have the source of the module but cannot find it.

The module.txt of the particular export module is:
Name: Import Export
Path: ImportExport
AntiForgery: enabled
Author: The Orchard Team
Website: http://orchardproject.net
Version: 1.6
OrchardVersion: 1.4
Description: Provides content item data import and export capability.
FeatureDescription: Imports and exports content item data
Category: Content

Thanks in advance :)

Jahn answered 15/2, 2013 at 9:12 Comment(0)
T
8

You need to override theExporting/Importing methods in your content part driver. Here is a simple example from the Orchard.Core.Title.Driver.TitlePartDriver:

protected override void Importing(TitlePart part, ImportContentContext context) {
    var title = context.Attribute(part.PartDefinition.Name, "Title");
    if (title != null) {
        part.Title = title;
    }
}

protected override void Exporting(TitlePart part, ExportContentContext context) {
    context.Element(part.PartDefinition.Name).SetAttributeValue("Title", part.Title);
}

The ImportExportContext class provides access to the underlying XML structure used to generate the output document, so if you are used to using System.Xml.Linq, XDocument etc then it will all seem familiar.

There are some other examples of usage in Orchard.Core.Common.Drivers.CommonPartDriver, Orchard.Users.Drivers.UserPartDriver, and Orchard.Comments.Drivers.CommentPartDriver.

Thwart answered 15/2, 2013 at 10:32 Comment(0)
G
0

You could try to use reflection:

        string[] notRequiredExportProperties = new string[] 
            {
                "Record", 
                "ContentItem", 
                "Zones", 
                "Id", 
                "TypeDefinition", 
                "TypePartDefinition",
                "PartDefinition", 
                "Settings", 
                "Fields"
            };

        protected override void Importing(ContactPart part, Orchard.ContentManagement.Handlers.ImportContentContext context)
        {
            var contactRecordType = part.Record.GetType();
            var allProps = contactRecordType.GetProperties();
            foreach (PropertyInfo p in allProps)
            {
                if (Array.FindIndex(notRequiredExportProperties, i => i == p.Name) > -1)
                    continue;

                var importValue = context.Attribute(part.PartDefinition.Name, p.Name);
                var import = Convert.ChangeType(importValue, p.PropertyType);
                if (p.PropertyType.IsSubclassOf(typeof(Enum)))
                    continue;
                p.SetValue(part.Record, import, null);
            }
        }

        protected override void Exporting(ContactPart part, Orchard.ContentManagement.Handlers.ExportContentContext context)
        {
            var contactPartType = part.GetType();
            var allProps = contactPartType.GetProperties();
            foreach(PropertyInfo p in allProps)
            {
                if (Array.FindIndex(notRequiredExportProperties, i => i == p.Name) > -1)
                    continue;

                var propVal = p.GetValue(part, null);
                context.Element(part.PartDefinition.Name).SetAttributeValue(p.Name, propVal);
            }
        }

You may have to do a bit extra to make it support Enums etc. This should probably be in a helper class of some sort.

Gasparo answered 20/1, 2015 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.