Custom part properties missing in export Orchard 1.6

帅比萌擦擦* 提交于 2020-01-03 10:46:09

问题


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 :)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/14891326/custom-part-properties-missing-in-export-orchard-1-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!