2

I'm planning to rename one of my custom content types, so that I can free up its name for a new Orchard module I am working on. I'm hoping to use Schema.ExecuteSql in a migration class as suggested by this SO answer, but I want to make sure I know all of the updates I will need to do.

So far, I understand that I need to update fields in the following tables:

  • Orchard_Framework_ContentTypeRecord
  • Settings_ContentTypeDefinitionRecord
  • Settings_ContentPartDefinitionRecord

Also, here is my general plan for the update SQL I will need to run:

DECLARE @From VARCHAR(50) = 'OriginalName'
DECLARE @To VARCHAR(50) = 'NewName'

BEGIN TRANSACTION

BEGIN TRY

    UPDATE [Current_Orchard_Framework_ContentTypeRecord]
    SET [Name] = @To
    WHERE [Name] = @From

    UPDATE [Current_Settings_ContentTypeDefinitionRecord]
    SET [Name] = @To, [DisplayName] = @To
    WHERE [Name] = @From

    UPDATE [dbo].[Current_Settings_ContentPartDefinitionRecord]
    SET [Name] = @To + 'Part'
    WHERE [Name] = @From + 'Part'

    --COMMIT TRANSACTION
    ROLLBACK TRANSACTION /*Rollback while testing*/
END TRY

BEGIN CATCH
    ROLLBACK TRANSACTION
END CATCH

Is there anything else I am missing that will need to be renamed to fully rename my content type?

Community
  • 1
  • 1
bingles
  • 11,582
  • 10
  • 82
  • 93
  • Is this really necessary? It sounds dangerous. You can always change the display name of a content type – Hazza Jan 30 '15 at 11:04
  • I plan to use my newer plugin for multiple sites of mine and want to let the content type become the one I use going forward. For that reason, I don't want to have to rename it in my new version. – bingles Jan 30 '15 at 15:27
  • Looks fine to me. Did you try it? – Bertrand Le Roy Jan 30 '15 at 22:51
  • The data change seemed to work ok, but it appears that there is now a mismatch with my content part class / record names and the text name stored in the db, which effectively orphans access to the content part. Also my content part table still bears the old name Current_XXX_XXX_XXXPartRecord. Guess I'm wondering if I should just name my new type something else rather than go through the pain of deprecating the old. – bingles Feb 02 '15 at 03:59
  • Did you get a way trough this? I'm faciing the same situation- – Pato Nov 10 '15 at 20:43
  • Unfortunately I never found a way that I trusted enough to go with. – bingles Nov 11 '15 at 00:59
  • After battling with this problem for quite a while in different projects, the easiest solution for me was always to use the import/export feature of orchard and simply modify the xml. Works like a charm. – Xceno Jan 27 '17 at 14:58

1 Answers1

0

This might not be the exact answer to your problem, but - although it has annecdotal character - it might help you find the solution.

I was working on a Orchard site that had

  • a lot of pages
  • a lot of html links to other pages on the site
  • a lot of custom link items in the navigations (to link to certain anchors inside a target page, etc.)

Shortly before we planned to delpoy the website, it was decided to change the URLs of the most prominent pages for SEO reasons from http://www.example.org/orchard/products/category-name/product-nameto http://www.example.org/orchard/shop/category-name/product-name-keyword-anotherkeyword(gotta love SEO...)

Doing this manually would have taken ages. Changing the URL in the content item itself would be easy (but doing that on ~20 items still would take some time), but changing all those <a hrefs in all the 120+ content items, and all the custom links in the 5 navigations (5 languages) would simply be impossible.

So the only feasable way seemed to be database manipulation.

I used MS SQL Server Management Studio to export all tables in the Orchard database to individual .sql files. I then used Notepad++ to search for all files that contained the partial string /products/category/product-name and wrote down the table names and the field name (usually Text or Data)

In this case the results were in the following tables:

  • Common_BodyPartRecord.Table
  • Orchard_Framework_ContentItemRecord
  • Orchard_Framework_ContentItemVersionRecord

To search-replace a partial string in SQl you use the replace function, and because of the datatype of the table fields the orchard stores the data in you have to cast strings. The resulting SQL queries look like this:

update [Orchard_Live].[dbo].[Common_BodyPartRecord] 
    set Text = cast(replace(cast(Text as nvarchar(max)),     N'/products/category/product-name', N'/shop/category-name/product-name-keyword-anotherkeyword') as ntext)
where cast(Text as nvarchar(max)) LIKE N'%/products/category/product-name%'

update [Orchard_Live].[dbo].[Orchard_Framework_ContentItemRecord] 
set Data = cast(replace(cast(Data as nvarchar(max)), N'/products/category/product-name', N'/shop/category-name/product-name-keyword-anotherkeyword') as ntext)
where cast(Data as nvarchar(max)) LIKE N'%/products/category/product-name%'

update [Orchard_Live].[dbo].[Orchard_Framework_ContentItemVersionRecord] 
set Data = cast(replace(cast(Data as nvarchar(max)), N'/products/category/product-name', N'/shop/category-name/product-name-keyword-anotherkeyword') as ntext)
where cast(Data as nvarchar(max)) LIKE N'%/products/category/product-name%'

I had to do this for every URL that had to be changed. Took quite a while, but still beat manually changing everything by far. Maybe this "workflow" can help with your problem, too.

Konadi
  • 182
  • 11