Un-Publish a list item programmatically

I wrestled with this one for a while. Although you can go to Version History and there is an option to “unpublish” a version doing this programmatically was very much a pain.

I finally realised that you could do this quite simply without going anywhere near the versioning end of the SharePoint API.

// overwrite the “published” version with a “draft” version
item.ModerationInformation.Status = SPModerationStatusType.Draft;

This did work in my instance where I knew the “current” version of the item was Published, although I’m not sure if this also works when you have draft versions in edit (i.e. version 1.5)