Simple Way To Move Document Types In 7.2.2 Without Losing Data
I have spent the day banging my head against this so I thought I would share this as I think it will be common issue until they update the admin panel.
If you want to move a Document Type so it is nested under a different Document Type then you have to be careful as doing it through copy, import or direct change to database can cause errors and loss of data on site.
The solution is very simple but I could not find it documented anywhere. The only way I found to do this safely is with the ContentTypeService API.
First you create references to the document you want to move, the destination and the current parent:
var docToMove = Services.ContentTypeService.GetContentType(id);
var currentParentType = docToMove.ParentId < 0 ? null : Services.ContentTypeService.GetContentType(docToMove.ParentId);
var newParentType = Services.ContentTypeService.GetContentType(newParent);
Then you set the parent ID of the doc type you want to move. Umbraco behind the scenes will then update the "umbracoNode" database table and update the path, level & sort order for you.
docToMove.ParentId = newParent;
The document will now be moved but it will not be inheriting the properties of its parent doc type because there will still be an entry in the "cmsContentType2ContentType" table pointing at the previous parent if it was already nested or no entry if it was not.
If the node is already nested and you try to just add the reference you will probably get errors as it may try to add a property alias that is already being inherited by the new parent as alias must be unique.
So first remove the existing reference and then add the new one. If the Document Type is at the root it will not be inheriting anything so if you try to remove you will get an error. Root doc types have parent id of -1 so make sure its a positive value.
if (currentParentType != null) docToMove.RemoveContentType(currentParentType.Alias);
docToMove.AddContentType(newParentType);
Then just call save:
Services.ContentTypeService.Save(docToMove);
Hope that helps someone. I added this code to a controller and used it to quickly re-organise my document types and everything seems to be working fine.
Is there any way of doing this from a razor view in umbraco 7?
If I add this
using Umbraco.Core;
@using Umbraco.Core.Services;
and this
var docToMove = Services.ContentTypeService.GetContentType(1104);
to my Home template, I get this error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'Services' does not exist in the current context
You can do it from Razor, but it's not recommended because Razor should only render html and not contain any logic. It's better to do something like this in a controller.
If you want to do it in Razor try this:
@using Umbraco.Core;
var docToMove = ApplicationContext.Current.Services.ContentTypeService.GetContentType(1104);
Simple Way To Move Document Types In 7.2.2 Without Losing Data
I have spent the day banging my head against this so I thought I would share this as I think it will be common issue until they update the admin panel.
If you want to move a Document Type so it is nested under a different Document Type then you have to be careful as doing it through copy, import or direct change to database can cause errors and loss of data on site.
The solution is very simple but I could not find it documented anywhere. The only way I found to do this safely is with the ContentTypeService API.
First you create references to the document you want to move, the destination and the current parent:
Then you set the parent ID of the doc type you want to move. Umbraco behind the scenes will then update the "umbracoNode" database table and update the path, level & sort order for you.
The document will now be moved but it will not be inheriting the properties of its parent doc type because there will still be an entry in the "cmsContentType2ContentType" table pointing at the previous parent if it was already nested or no entry if it was not.
If the node is already nested and you try to just add the reference you will probably get errors as it may try to add a property alias that is already being inherited by the new parent as alias must be unique.
So first remove the existing reference and then add the new one. If the Document Type is at the root it will not be inheriting anything so if you try to remove you will get an error. Root doc types have parent id of -1 so make sure its a positive value.
Then just call save:
Hope that helps someone. I added this code to a controller and used it to quickly re-organise my document types and everything seems to be working fine.
Hi
Is there any way of doing this from a razor view in umbraco 7?
If I add this
using Umbraco.Core;
@using Umbraco.Core.Services;
and this
var docToMove = Services.ContentTypeService.GetContentType(1104);
to my Home template, I get this error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'Services' does not exist in the current context
Any ideas?
Best regards, Jan
Hello,
You can do it from Razor, but it's not recommended because Razor should only render html and not contain any logic. It's better to do something like this in a controller.
If you want to do it in Razor try this:
Jeroen
is working on a reply...