Monday, 16 March 2009

Um Bongo

Does anyone else have the Um Bongo tune constantly going through their head when working with Umbraco?

Programatically publishing, unpublishing, deleting and renaming nodes in Umbraco

I have an ActionHandler that publishes, unpublishes, deletes and renames nodes (documents) in Umbraco.

At first my published items would appear in the content tree but not in the website and my unpublished or deleted items would grey out or disappear from the content tree but not the website.

I dug around the forums for a bit and found the correct method calls - here they are:

Publish
var myNode = new Document(id);
myNode.Publish(umbraco.helper.GetCurrentUmbracoUser());
umbraco.library.UpdateDocumentCache(myNode.Id);
Unpublish
var myNode = new Document(id);
myNode.UnPublish();
umbraco.library.UnPublishSingleNode(myNode.Id);
Delete
var myNode = new Document(id);
myNode.delete();
umbraco.library.RefreshContent();
Rename
var myNode = new Document(id);
var versions = myNode.GetVersions();
if (versions != null && versions.Length > 1)
{
var previousVersion = versions[versions.Length - 2];
if (myNode.Text != previousVersion.Text)
{
// This is a rename, as an example I will just make sure the new name is lower case...
myNode.Text = myNode.Text.ToLower();
if (myNode.Published)
{
myNode.Publish(umbraco.helper.GetCurrentUmbracoUser());
umbraco.library.UpdateDocumentCache(myNode.Id);
}
}
}
There is no IAction class for renames so I just use ActionUpdate.Instance. My ReturnActions() method looks like this - 
public IAction[] ReturnActions()
{
return new IAction[] {
ActionPublish.Instance,
ActionDelete.Instance,
ActionUnPublish.Instance,
ActionUpdate.Instance
};
}