From 53ee69e682ab201027208a183ac8bd6e6eeb898a Mon Sep 17 00:00:00 2001 From: ustadstar <36473707+ustadstar@users.noreply.github.com> Date: Sun, 8 Jan 2023 16:10:56 +0100 Subject: [PATCH] Fix copy content with descendants not copying sort order (#13464) --- src/Umbraco.Core/Constants-SqlTemplates.cs | 1 + .../Implement/ContentRepositoryBase.cs | 15 +++++++++++++++ .../Repositories/Implement/DocumentRepository.cs | 5 +++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Core/Constants-SqlTemplates.cs b/src/Umbraco.Core/Constants-SqlTemplates.cs index 549dae5bd62f..ad5b32603571 100644 --- a/src/Umbraco.Core/Constants-SqlTemplates.cs +++ b/src/Umbraco.Core/Constants-SqlTemplates.cs @@ -10,6 +10,7 @@ public static class VersionableRepository public const string GetVersion = "Umbraco.Core.VersionableRepository.GetVersion"; public const string GetVersions = "Umbraco.Core.VersionableRepository.GetVersions"; public const string EnsureUniqueNodeName = "Umbraco.Core.VersionableRepository.EnsureUniqueNodeName"; + public const string SortOrderExists = "Umbraco.Core.VersionableRepository.SortOrderExists"; public const string GetSortOrder = "Umbraco.Core.VersionableRepository.GetSortOrder"; public const string GetParentNode = "Umbraco.Core.VersionableRepository.GetParentNode"; public const string GetReservedId = "Umbraco.Core.VersionableRepository.GetReservedId"; diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs index ce14f1ec1e0b..de247a61205b 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs @@ -1013,6 +1013,21 @@ public TempContent(int id, int versionId, int publishedVersionId, IContentTypeCo return SimilarNodeName.GetUniqueName(names, id, nodeName); } + protected virtual bool SortorderExists(int parentId, int sortOrder) + { + SqlTemplate? template = SqlContext.Templates.Get(Constants.SqlTemplates.VersionableRepository.SortOrderExists, tsql => tsql + .Select("sortOrder") + .From() + .Where(x => x.NodeObjectType == SqlTemplate.Arg("nodeObjectType") && + x.ParentId == SqlTemplate.Arg("parentId") && + x.SortOrder == SqlTemplate.Arg("sortOrder"))); + + Sql sql = template.Sql(NodeObjectTypeId, parentId, sortOrder); + var result = Database.ExecuteScalar(sql); + + return result != null; + } + protected virtual int GetNewChildSortOrder(int parentId, int first) { SqlTemplate? template = SqlContext.Templates.Get(Constants.SqlTemplates.VersionableRepository.GetSortOrder, tsql => tsql diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs index 5c7488401f5b..3951ffba6928 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs @@ -900,8 +900,9 @@ protected override void PersistNewItem(IContent entity) NodeDto parent = GetParentNodeDto(entity.ParentId); var level = parent.Level + 1; - // get sort order - var sortOrder = GetNewChildSortOrder(entity.ParentId, 0); + var sortOrderExists = SortorderExists(entity.ParentId, entity.SortOrder); + // if the sortorder of the entity already exists get a new one, else use the sortOrder of the entity + var sortOrder = sortOrderExists ? GetNewChildSortOrder(entity.ParentId, 0) : entity.SortOrder; // persist the node dto NodeDto nodeDto = dto.ContentDto.NodeDto;