orchardcmsorchardcms-1.9

How do I programatically copy Taxonomy Terms in Orchard 1.9?


I am trying to programmatically clone content items. The built-in clone() method isn't sufficient, I need to interfere with part of process. The content items I'm trying to clone have a Category Taxonomy Field. I was hoping I could do something along the lines of:

        var sourceContentItemCategory = sourceContentItem.ContentItem.Category as TaxonomyField;
        var targetContentItemCategory = targetContentItem.ContentItem.Category as TaxonomyField;

        targetContentItemCategory.Terms = sourceContentItemCategory.Terms;

But I understand why that doesn't work. Terms are readonly and if I'm not mistaken, every term is also its own content item, so I can't just wholesale stuff that into another taxonomy. What's the easiest way to go about this? Can the TaxonomyService help me make this painless?


Solution

  • I figured out that this can pretty easily be done with the help of the Taxonomy Service:

    var sourceTerms = sourceItem.Item.Category.Terms;
    _taxonomyService.UpdateTerms(targetItem, sourceTerms, "[Name of Taxonomy Field = Category]");