This project is read-only.
3

Resolved

ContentItems do not support Import/Export

description

The content items do not support import/export logic making the migration of data from test to prod difficult. I've tried to implement this myself but because it stores a reference to the actual content item rather than the alias or just uses a container object it does not serialize well.

comments

RichardGarside wrote Mar 16, 2012 at 10:53 AM

I've been rushed off my feet since 1.4 came out and not had a chance to test the update path. I'll try to look at this over the weekend.

corecomps wrote Mar 16, 2012 at 12:53 PM

Thanks Richard. Just to be clear I'm talking about the ability to import and export instances of the content items through the import/export module by overriding the importing and exporting methods:

Something like this:
    protected override void Exporting(BingMapListPart part, ExportContentContext context)
    {
        context.Element(part.PartDefinition.Name).SetAttributeValue("CenterLatitude", part.CenterLatitude);
        context.Element(part.PartDefinition.Name).SetAttributeValue("CenterLongitude", part.CenterLongitude);
        context.Element(part.PartDefinition.Name).SetAttributeValue("Width", part.Width);
        context.Element(part.PartDefinition.Name).SetAttributeValue("Height", part.Height);
        context.Element(part.PartDefinition.Name).SetAttributeValue("Zoom", part.Zoom);
        context.Element(part.PartDefinition.Name).SetAttributeValue("MapType", part.MapType);
        ********context.Element(part.PartDefinition.Name).SetAttributeValue("BingLocations", SerializeToString(part.BingLocations.ToList()));*******
    }
Note, when I tried, I was hung up by the fact that the content types reference lists of other types directly rather than using container or alias as a reference. The result is a difficulty serializing and maintain the referential integrity between the Map and the Locations attached to it.

RichardGarside wrote Mar 18, 2012 at 3:44 PM

Ah sorry. Thought you were talking about updating from 1.3 -> 1.4. That seems to blow things up at the moment.

I'm full of cold this weekend and struggling to think straight. Have you tried looking at:
http://developer.3l.nl/post/16165483880/orchard-import-export-1-n-relations

Let me know how you get on. I need to fix my upgrade issue first.

jao28 wrote May 21, 2012 at 4:58 PM

I went ahead and coded the Import/Export process (tested and working for me). The following code needs added in BingLocationDriver.cs:

protected override void Exporting(BingLocationPart part, ExportContentContext context) {
context.Element(part.PartDefinition.Name).SetAttributeValue("Latitude", part.Latitude.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("Longitude", part.Longitude.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("IsEnabled", part.IsEnabled.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("Width", part.Width.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("Height", part.Height.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("Zoom", part.Zoom.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("MapType", part.MapType);
context.Element(part.PartDefinition.Name).SetAttributeValue("MapIcon", part.MapIcon);

if (part.Record.BingMapList != null) {
    var bingMapListPart = _services.ContentManager.Query<BingMapListPart, BingMapListRecord>()
        .Where(x => x.Id == part.Record.BingMapList.Id).List().FirstOrDefault();
    if (bingMapListPart != null) {
        var bingMapListIdentity = _services.ContentManager.GetItemMetadata(bingMapListPart).Identity;
        context.Element(part.PartDefinition.Name).SetAttributeValue("BingMapList", bingMapListIdentity.ToString());
    }
}
}

protected override void Importing(BingLocationPart part, ImportContentContext context) {
var latitudeString = context.Attribute(part.PartDefinition.Name, "Latitude");
if (!String.IsNullOrWhiteSpace(latitudeString)) {
    float latitude = 0;
    if (float.TryParse(latitudeString, out latitude)) {
        part.Latitude = latitude;
    }
}
var longitudeString = context.Attribute(part.PartDefinition.Name, "Longitude");
if (!String.IsNullOrWhiteSpace(longitudeString)) {
    float longitude = 0;
    if (float.TryParse(longitudeString, out longitude)) {
        part.Longitude = longitude;
    }
}
var isEnabledString = context.Attribute(part.PartDefinition.Name, "IsEnabled");
if (!String.IsNullOrWhiteSpace(isEnabledString)) {
    bool isEnabled = false;
    if (bool.TryParse(isEnabledString, out isEnabled)) {
        part.IsEnabled = isEnabled;
    }
}
var widthString = context.Attribute(part.PartDefinition.Name, "Width");
if (!String.IsNullOrWhiteSpace(widthString)) {
    int width = 0;
    if (int.TryParse(widthString, out width)) {
        part.Width = width;
    }
}
var heightString = context.Attribute(part.PartDefinition.Name, "Height");
if (!String.IsNullOrWhiteSpace(heightString)) {
    int height = 0;
    if (int.TryParse(heightString, out height)) {
        part.Height = height;
    }
}
var zoomString = context.Attribute(part.PartDefinition.Name, "Zoom");
if (!String.IsNullOrWhiteSpace(zoomString)) {
    int zoom = 0;
    if (int.TryParse(zoomString, out zoom)) {
        part.Zoom = zoom;
    }
}
var mapType = context.Attribute(part.PartDefinition.Name, "MapType");
part.MapType = mapType;
var mapIcon = context.Attribute(part.PartDefinition.Name, "MapIcon");
part.MapIcon = mapIcon;    
}

protected override void Imported(BingLocationPart part, ImportContentContext context) {
// assign the bingMapList only when everything is imported
var bingMapList = context.Attribute(part.PartDefinition.Name, "BingMapList");
if (bingMapList != null) {
    part.Record.BingMapList = context.GetItemFromSession(bingMapList).As<BingMapListPart>().ContentItem.Record;
}
}


The following code needs added in BingMapListDriver.cs:

protected override void Exporting(BingMapListPart part, ExportContentContext context) {
context.Element(part.PartDefinition.Name).SetAttributeValue("CenterLatitude", part.CenterLatitude.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("CenterLongitude", part.CenterLongitude.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("Width", part.Width.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("Height", part.Height.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("Zoom", part.Zoom.ToString());
context.Element(part.PartDefinition.Name).SetAttributeValue("MapType", part.MapType);
}

protected override void Importing(BingMapListPart part, ImportContentContext context) {
var centerLatitudeString = context.Attribute(part.PartDefinition.Name, "CenterLatitude");
if (!String.IsNullOrWhiteSpace(centerLatitudeString)) {
    float centerLatitude = 0;
    if (float.TryParse(centerLatitudeString, out centerLatitude)) {
        part.CenterLatitude = centerLatitude;
    }
}
var centerLongitudeString = context.Attribute(part.PartDefinition.Name, "CenterLongitude");
if (!String.IsNullOrWhiteSpace(centerLongitudeString)) {
    float centerLongitude = 0;
    if (float.TryParse(centerLongitudeString, out centerLongitude)) {
        part.CenterLongitude = centerLongitude;
    }
}
var widthString = context.Attribute(part.PartDefinition.Name, "Width");
if (!String.IsNullOrWhiteSpace(widthString)) {
    int width = 0;
    if (int.TryParse(widthString, out width)) {
        part.Width = width;
    }
}
var heightString = context.Attribute(part.PartDefinition.Name, "Height");
if (!String.IsNullOrWhiteSpace(heightString)) {
    int height = 0;
    if (int.TryParse(heightString, out height)) {
        part.Height = height;
    }
}
var zoomString = context.Attribute(part.PartDefinition.Name, "Zoom");
if (!String.IsNullOrWhiteSpace(zoomString)) {
    int zoom = 0;
    if (int.TryParse(zoomString, out zoom)) {
        part.Zoom = zoom;
    }
}
var mapType = context.Attribute(part.PartDefinition.Name, "MapType");
part.MapType = mapType;
}

Thanks for this great module and hopefully you can drop this in the source code. I am also going to drop this in a discussion so it is easier to grab the code from.

jao28 wrote May 21, 2012 at 5:01 PM

Final comment, don't forget to add

using Orchard.ContentManagement.Handlers;

to both of these drivers.

RichardGarside wrote Jul 11, 2014 at 12:07 PM

Fixed in changeset e7a437d0eeee