Accidentally deleted a content type -- how to recover?

Topics: Troubleshooting
Oct 17, 2012 at 10:44 PM

Hello there,

I meant to remove a content part at the bottom of my content type edit page. However, instead of clicking "Remove" to delete the part, I accidentally hit "Delete" and deleted my Blog Post content type.

How can I get this back without reinstalling my site? Also, is the content associated with the blog post type still in here somewhere? In other words, is this like a broken link, where my posts will reappear if I make the connections (reupload the Blog Post type code), or are the actual posts a child of the content type, gone unless backed up?

Thank you,
MM

Coordinator
Oct 17, 2012 at 10:47 PM

First, make a backup of your database. Then you can attempt to re-create the type. No guarantee however. Your data is still in there: Orchard does soft-deletes.

Oct 19, 2012 at 3:15 PM

Hi Bertrand, thanks for the advice. I'm using Mercurial to manage the branches now.

I've recovered the posts themselves by adding Version Manager, which installed a Recycle Bin in my Content section. Still working to recover the actual blogs. I can see the page generated for "/press-releases/article-name," but when I visit "/press-releases"

I see a 500 HTML error. Will come back to record when I find a solution in case anyone else finds themselves in a similar situation.

Coordinator
Oct 19, 2012 at 4:02 PM

500 does not tell you much. You need to have the full stack trace, at least (you can get it from app_data\logs), and you can also attach a debugger and figure out what's gone wrong.

Oct 19, 2012 at 10:58 PM
Edited Oct 19, 2012 at 11:07 PM
bertrandleroy wrote:

500 does not tell you much.

True, just describing details of what I see in case anyone else encounters the same pattern, whether now or in a future case.

bertrandleroy wrote:

500 does not tell you much. You need to have the full stack trace, at least (you can get it from app_data\logs), and you can also attach a debugger and figure out what's gone wrong.

Bertrand, in response to your previous post, I recreated to create a new type called Blog Post (BlogPost), but it was unsuccessful. My site does not see it as the same type as before.

My posts still exists, but the blogs are gone, so I then tried to create a new blog. I can now see the main page for this blog, with its description / tagline and the info "There are no posts for this blog." I was unable to move any post to this blog. Furthermore, even when I created a new post for this blog, it does not show up on the main page for this blog.

---

I have an approximate time that the error occurred, so I'll examine the stack data as you suggested.

Developer
Nov 1, 2012 at 2:04 AM

 When you re-created the content type, did you also fix up the content type if values of the content items? Simply creating a type with the same name will not be enough.

Nov 21, 2012 at 2:50 AM

There is a table called Orchard_Framework_ContentTypeRecord. You will see that there appears the name of the deleted content type (and some id, remember it). There is another table named Orchard_Framework_ContentItemRecord wich has a column called ContentType_id referencing the id of the content type for each item in Orchard. However your content type will not be shown in content type list because it is deleted and any query by content type will return blank.

SOLUTION: First of all, you need to create a new Content Type called exactly as your previously content type (can be done manually by the Orchard Admin UI).

This newly created content type will be added to a table called Settings_ContentTypeDefinitionRecord (settings for the content type) and table Settings_ContentTypePartDefinitionRecord (relations between content type and content parts). Orchard will map the name of the content type with the name of content type in table Orchard_Framework_ContentTypeRecord to find its associated ID. So far so good but you still will not see any items in orchard. That is because all items of this content type were marked as unpublished when you delete the content type. So, you need to “hack” your DB and set Publised=1 and Latest=1 when applicable in table Orchard_Framework_ContentItemVersionRecord. To do so you can use the following query (suppose deleted content type id = 13, find it in table Orchard_Framework_ContentTypeRecord):

UPDATE     Orchard_Framework_ContentItemVersionRecord
SET        Published = 1, Latest = 1
FROM       Orchard_Framework_ContentItemVersionRecord 
INNER JOIN Orchard_Framework_ContentItemRecord ON Orchard_Framework_ContentItemVersionRecord.ContentItemRecord_id = Orchard_Framework_ContentItemRecord.Id 
INNER JOIN Orchard_Framework_ContentTypeRecord ON Orchard_Framework_ContentItemRecord.ContentType_id = Orchard_Framework_ContentTypeRecord.Id
WHERE  (Orchard_Framework_ContentTypeRecord.Id = 13)
		AND (SELECT MAX(Id) FROM Orchard_Framework_ContentItemVersionRecord AS ItemVersionRecord WHERE ItemVersionRecord.ContentItemRecord_id = Orchard_Framework_ContentItemVersionRecord.ContentItemRecord_id)<=Orchard_Framework_ContentItemVersionRecord.Id 

This is all you need to do. Enjoy it at your own risk (make a backup of your DB before run this update query). NOTE: The subquery in WHERE will ensure that Published=1 only apply in the latest added content when multiple versions exists for the same content item.