Sitecore Blog: @sitecorejohn blog

Repost: Publish Referenced Media When You Publish Sitecore Content

By John West, November 16, 2010 | Rating:  | Comments (4)

Screen Shot of Media Publishing Workflow definition

This blog post provides describes a prototype I developed for use with the Sitecore ASP.NET CMS to publish referenced media automatically as a content item passes through workflow. You can use this workflow action to publish media items and media folders automatically before publishing content that depends on those items.

This is a repost of http://sitecorejohn.spaces.live.com/blog/cns!960125F1D4A59952!829.entry.

See also Alex Shyba's blog post Publish related media items for more information, and Hidden feature of Sitecore 6.2 for information about an alternative coding approach for Sitecore 6.2 and later.

You can replace the default Auto Publish workflow action with this action. Additionally, you can use this workflow action earlier in the workflow, from a non-final workflow state, to publish only the media and not the content item. The Publish Media and Publish Content actions both use a single action implementation described in this post.

From the Draft state, the user submits a content item to the Media Approval state. From the Media Approval state, the media reviewer chooses the Approve Media command, which contains the Publish Media action that publishes referenced media, and transitions to the Content Approval state. From the Content Approval state, the content reviewer (who may also be the media reviewer) chooses the Approve Content commend, which transitions to the Approved state. The Approved state contains the Publish Content action that publishes the item and its descendants. The Validation Action under the Approve Content command prevents the content reviewer from publishing invalid content. Content can be invalid for a number of reasons, such as if you Validate Media Publishing Status with Sitecore and the content reviewer added a new image after media approval.

In the Parameters field in the Data section of the Publish Media workflow action definition item, enter:

IncludeSelf=1&Deep=1&IncludeMedia=1

The IncludeSelf parameter instructs the action to publish the item in workflow. The Deep parameter instructs the action to perform deep publishing. The IncludeMedia parameter instructs the action to publish related media. Alternatively, you can create a data template that defines three Checkbox fields named after these three parameters, use that data template for the workflow action definition item, and use those fields instead of the Parameters field.

In the Parameters field in the Data section of the Publish Content workflow action definition item, enter:

IncludeMedia=1

The IncludeSelf and Deep parameters default to false.

I will post the code at the Workflow with Media Items thread on the Sitecore Developer Network forums.

The Sitecore.Sharedsource.Workflow.Actions.Publish class that you reference in the Type field of workflow action definition items represents the workflow action. You can use this action to replace the default Sitecore.Workflows.Simple.PublishAction. Most of the logic in this class simply retrieves parameters. The Process() method passes those parameters to the Publish() method of an instance of Sitecore.Publishing.PublishHelper.

For each publishing target, the Publish() method of Sitecore.Publishing.PublishHelper creates a publishing queue containing the media referenced by the item in workflow that do not exist or differ from their correspondents in the publishing target database and any of their ancestors missing from that publishing target, and then publishes that queue.

The Sitecore.Publishing.PublishingTarget class represents a publishing target.

Even accounting for publishing targets, publishing related media is relatively straightforward if media will not have publishing restrictions, you don’t version/workflow media, early publication of media and media folders is unlikely to have a negative impact, and media items don’t have descendants. While this solution addresses the complexity of publishing targets, it does not address versioning, translation, publishing restrictions, and other factors. Nor does it address errors that can occur during publication.

Publishing related content would be more challenging than publishing related media. Referenced content may have publishing restrictions at the item and/or language/version level, including workflow state. Referenced content may contains references to additional media and content, including circular references. Still, it might be possible to publish related content, presentation components, data template definition components, or other relevant items.

At some point I tried to address some of these concerns with the PublishingSpider Sitecore Shared Source project, but I think it got too complicated and I didn’t maintain it. Maybe that project has some ideas or even code that someone reading this blog post could use.

Tags: API, Architecture, Infrastructure

Comments

  • Hey John,
    I extended this code to publish all media items of the descendant items as well, only if Deep==1 and IncludeMedia==1.
    This means there won't be any missing images on the pages of the children of the item you're publishing/
    I don't know if there is anything wrong with doing such a thing, but it definitely helped with the project I am currently working on.

    To do this, I changed the 'BuildQueue' function in PublishHelper from this:-

    if (includeMedia)
    {
    foreach (Item media in this.GetUnpublishedMedia(item, target))
    {
    this.AddToQueue(context, media);
    }
    }
    return context;


    To this:-

    if (includeMedia)
    {
    foreach (Item media in this.GetUnpublishedMedia(item, target))
    {
    this.AddToQueue(context, media);
    }

    if (deep)
    {
    Item[] alldescendants = item.Axes.GetDescendants();
    foreach (Item descendant in alldescendants)
    {
    foreach (Item media in this.GetUnpublishedMedia(descendant, target))
    {
    this.AddToQueue(context, media);
    }
    }
    }
    }

    return context;


    Let me know what you think.

    - Sean Holmesby
    November 29, 2010 at 8:15 PM

  • Thank you so much for this post! This problem has been hounding us since we launched Sitecore a year ago. Such a simple solution to what has been a nagging problem.

    - Vickie Bertini
    December 07, 2010 at 11:24 AM

  • Has anyone had any success with this on Sitecore 6.3.0 or higher along with the new scaling feature?

    - Janrey Ocfemia
    January 17, 2011 at 9:31 PM

  • iWdyty Author, keep doing in the same way!!

    - Elizabeth@gmail.com Elizabeth@gmail.com
    August 11, 2011 at 2:29 AM

*
*
*

Learn More with Sitecore

Newsletter
*

ISS | Read Case Study >

Sitecore Intranet Portal allows us to integrate other systems so seamlessly that users think it’s all on the same system. It’s a great feature – and essential for us.

- Birgit Fagerholt, ISS Facility Services A/S