SDL Tridion 2011 Page Publisher

Towards the end of last year I needed to create a quick and simple replica of the SDL Tridion 2009 Page Publisher Power Tool for SDL Tridion 2011. Here’s how I did it:

Create a page with the following:

  • A text box for the TCM Id of the Structure Group containing the pages you would like to publish
  • A text box for the TCM Id of the publishing target to publish to
  • A Yes/No radio button list for publishing child Structure Groups
  • A Yes/No radio button list for publishing to child Publications
  • A Yes/No radio button list for publishing the Components on the Pages
  • A Low/Normal/High radio button list for the publishing priority
  • A button to press to start the publishing
  • A label to display success/error messages

Page Publisher Controls

In the code behind, create a method witch uses each one of the settings above to publish the pages in the Structure Group specified. If "Publish Child Structure Groups" is set to Yes then the code recursively calls itself:

[code]
private void PublishPages(string sgID, ICoreService2010 client)
{
//publication target id
string targetID = txtPublicationTargetID.Text;

//publish to child publications?
bool publishChildren = false;
if (rblChildPublications.SelectedValue == "Yes")
{
publishChildren = true;
}

//create the publish instructions
var pubData = new PublishInstructionData
{
ResolveInstruction = new ResolveInstructionData() { IncludeChildPublications = publishChildren },
RenderInstruction = new RenderInstructionData()
};

//create a structure group filter
OrganizationalItemItemsFilterData sgFilter = new OrganizationalItemItemsFilterData
{
ItemTypes = new[] { ItemType.StructureGroup }
};

//create a page filter
OrganizationalItemItemsFilterData pageFilter = new OrganizationalItemItemsFilterData
{
ItemTypes = new[] { ItemType.Page }
};

//figure out the publish priority
PublishPriority priority = PublishPriority.Normal;
switch (rblPriority.SelectedValue)
{
case "Low":
priority = PublishPriority.Low;
break;
case "Normal":
priority = PublishPriority.Normal;
break;
case "High":
priority = PublishPriority.High;
break;
default:
priority = PublishPriority.Normal;
break;
}

//find any pages in the structure group specified
XElement pages = client.GetListXml(sgID, pageFilter);
foreach (var page in pages.Elements())
{
//publish the page
var pageId = page.Attribute("ID").Value;
client.Publish(new[] { pageId }, pubData, new[] { targetID }, priority, null);

//publish the components too?
if (rblComponents.SelectedValue == "Yes")
{
//get the page
PageData p = (PageData)client.Read(pageId, null);
//loop through each Component Presentation on the page and publish them
foreach (var cp in p.ComponentPresentations)
{
client.Publish(new[] { cp.Component.IdRef }, pubData, new[] { targetID }, priority, null);
}
}
}

//loop through child Structure Groups?
if (rblChildStructureGroups.SelectedValue == "Yes")
{
//get the structure group ids
XElement structureGroups = client.GetListXml(sgID, sgFilter);
foreach (var structureGroup in structureGroups.Elements())
{
//call the publish pages method
PublishPages(structureGroup.Attribute("ID").Value, client);
}
}
}
[/code]

On click of the publish button, call the publish method created above, passing in the starting Structure Group Id and the CoreService client to use. The GetTridionClient() method is taken from Frank van Puffelen's article at http://code.google.com/p/tridion-practice/wiki/GetCoreServiceClientWithoutConfigFile

[code]
protected void btnPublish_Click(object sender, EventArgs e)
{
lblMessage.Text = "";
try
{
PublishPages(txtSGID.Text, Utilities.GetTridionClient());
lblMessage.Text = "Successfully published the pages";
}
catch (Exception ex)
{
lblMessage.Text = "Error publishing pages: " + ex.Message;
}
}
[/code]

Any questions?

If you need more information or have any questions just get in touch and we'd be happy to answer them for you.