Importing/Exporting ContentItem Relationships

Topics: Customizing Orchard, Writing modules
Jul 9, 2011 at 11:23 PM

I started looking into the recently added import/export features added to Orchard. I'd like to export a non-trivial relationship of objects (and reimport them again :)). Say we have classes ParentPart and ChildPart, where ChildPart has a reference to ParentPart. I skimmed through the Orchard source, and found one place where this is performed:

Orchard.Core.Common.Drivers.CommonPartDriver

(From Importing())

var container = context.Attribute(part.PartDefinition.Name, "Container");
if (container != null) {
    part.Container = context.GetItemFromSession(container);
}

(From Exporting())

if (part.Container != null) {
    var containerIdentity = 
    _contentManager.GetItemMetadata(part.Container).Identity;
    context.Element(part.PartDefinition.Name)
           .SetAttributeValue("Container", containerIdentity.ToString()); 
}

I see here that the CommonPartDriver.Importing() method tries to lookup the Container for the CommonPart from the Import session. Looking a bit more, I see that in DataRecipeHandler.ExecuteRecipeStep(), which seems to perform the import, it iterates over all the objects once and creates objects using their ContentIdentities, and then imports them one by one using the ContentManager.Import() method; this allows the context.GetItemFromSession() method call from within a Importing() call to return the appropriate object so we can set the reference. The remaining part to the puzzle is how to properly set up a ContentIdentity object to represent the object that will be referred to. A quick scan of the source reveals that this happens in the Handler.GetItemMetadata() method:

context.Metadata.Identity.Add("MyPart.MyProperty", part.Name);

So it looks like we should simply set up our ContentIdentity to be the sum of all fields we feel unique represents our object.

My question is basically if I am missing anything, or if there are otherwise any points one should be careful about when creating imports/exports for custom parts.

Cheers.