@@ -546,13 +546,33 @@ private Dictionary<string, int> CreateContentTypeFolderStructure(IEnumerable<XEl
546
546
{
547
547
var alias = documentType . Element ( "Info" ) . Element ( "Alias" ) . Value ;
548
548
var folders = foldersAttribute . Value . Split ( Constants . CharArrays . ForwardSlash ) ;
549
+
550
+ var folderKeysAttribute = documentType . Attribute ( "FolderKeys" ) ;
551
+
552
+ var folderKeys = Array . Empty < Guid > ( ) ;
553
+ if ( folderKeysAttribute != null )
554
+ {
555
+ folderKeys = folderKeysAttribute . Value . Split ( Constants . CharArrays . ForwardSlash ) . Select ( x=> Guid . Parse ( x ) ) . ToArray ( ) ;
556
+ }
557
+
549
558
var rootFolder = WebUtility . UrlDecode ( folders [ 0 ] ) ;
550
- //level 1 = root level folders, there can only be one with the same name
551
- var current = _contentTypeService . GetContainers ( rootFolder , 1 ) . FirstOrDefault ( ) ;
559
+
560
+ EntityContainer current ;
561
+ Guid ? rootFolderKey = null ;
562
+ if ( folderKeys . Length == folders . Length && folderKeys . Length > 0 )
563
+ {
564
+ rootFolderKey = folderKeys [ 0 ] ;
565
+ current = _contentTypeService . GetContainer ( rootFolderKey . Value ) ;
566
+ }
567
+ else
568
+ {
569
+ //level 1 = root level folders, there can only be one with the same name
570
+ current = _contentTypeService . GetContainers ( rootFolder , 1 ) . FirstOrDefault ( ) ;
571
+ }
552
572
553
573
if ( current == null )
554
574
{
555
- var tryCreateFolder = _contentTypeService . CreateContainer ( - 1 , rootFolder ) ;
575
+ var tryCreateFolder = _contentTypeService . CreateContainer ( - 1 , rootFolderKey ?? Guid . NewGuid ( ) , rootFolder ) ;
556
576
if ( tryCreateFolder == false )
557
577
{
558
578
_logger . LogError ( tryCreateFolder . Exception , "Could not create folder: {FolderName}" , rootFolder ) ;
@@ -567,7 +587,8 @@ private Dictionary<string, int> CreateContentTypeFolderStructure(IEnumerable<XEl
567
587
for ( var i = 1 ; i < folders . Length ; i ++ )
568
588
{
569
589
var folderName = WebUtility . UrlDecode ( folders [ i ] ) ;
570
- current = CreateContentTypeChildFolder ( folderName , current ) ;
590
+ Guid ? folderKey = ( folderKeys . Length == folders . Length ) ? folderKeys [ i ] : null ;
591
+ current = CreateContentTypeChildFolder ( folderName , folderKey ?? Guid . NewGuid ( ) , current ) ;
571
592
importedFolders [ alias ] = current . Id ;
572
593
}
573
594
}
@@ -576,17 +597,17 @@ private Dictionary<string, int> CreateContentTypeFolderStructure(IEnumerable<XEl
576
597
return importedFolders ;
577
598
}
578
599
579
- private EntityContainer CreateContentTypeChildFolder ( string folderName , IUmbracoEntity current )
600
+ private EntityContainer CreateContentTypeChildFolder ( string folderName , Guid folderKey , IUmbracoEntity current )
580
601
{
581
602
var children = _entityService . GetChildren ( current . Id ) . ToArray ( ) ;
582
- var found = children . Any ( x => x . Name . InvariantEquals ( folderName ) ) ;
603
+ var found = children . Any ( x => x . Name . InvariantEquals ( folderName ) || x . Key . Equals ( folderKey ) ) ;
583
604
if ( found )
584
605
{
585
606
var containerId = children . Single ( x => x . Name . InvariantEquals ( folderName ) ) . Id ;
586
607
return _contentTypeService . GetContainer ( containerId ) ;
587
608
}
588
609
589
- var tryCreateFolder = _contentTypeService . CreateContainer ( current . Id , folderName ) ;
610
+ var tryCreateFolder = _contentTypeService . CreateContainer ( current . Id , folderKey , folderName ) ;
590
611
if ( tryCreateFolder == false )
591
612
{
592
613
_logger . LogError ( tryCreateFolder . Exception , "Could not create folder: {FolderName}" , folderName ) ;
@@ -1057,17 +1078,26 @@ private Dictionary<string, int> CreateDataTypeFolderStructure(IEnumerable<XEleme
1057
1078
foreach ( var datatypeElement in datatypeElements )
1058
1079
{
1059
1080
var foldersAttribute = datatypeElement . Attribute ( "Folders" ) ;
1081
+
1060
1082
if ( foldersAttribute != null )
1061
1083
{
1062
1084
var name = datatypeElement . Attribute ( "Name" ) . Value ;
1063
1085
var folders = foldersAttribute . Value . Split ( Constants . CharArrays . ForwardSlash ) ;
1086
+ var folderKeysAttribute = datatypeElement . Attribute ( "FolderKeys" ) ;
1087
+
1088
+ var folderKeys = Array . Empty < Guid > ( ) ;
1089
+ if ( folderKeysAttribute != null )
1090
+ {
1091
+ folderKeys = folderKeysAttribute . Value . Split ( Constants . CharArrays . ForwardSlash ) . Select ( x=> Guid . Parse ( x ) ) . ToArray ( ) ;
1092
+ }
1064
1093
var rootFolder = WebUtility . UrlDecode ( folders [ 0 ] ) ;
1094
+ var rootFolderKey = folderKeys . Length > 0 ? folderKeys [ 0 ] : Guid . NewGuid ( ) ;
1065
1095
//there will only be a single result by name for level 1 (root) containers
1066
1096
var current = _dataTypeService . GetContainers ( rootFolder , 1 ) . FirstOrDefault ( ) ;
1067
1097
1068
1098
if ( current == null )
1069
1099
{
1070
- var tryCreateFolder = _dataTypeService . CreateContainer ( - 1 , rootFolder ) ;
1100
+ var tryCreateFolder = _dataTypeService . CreateContainer ( - 1 , rootFolderKey , rootFolder ) ;
1071
1101
if ( tryCreateFolder == false )
1072
1102
{
1073
1103
_logger . LogError ( tryCreateFolder . Exception , "Could not create folder: {FolderName}" , rootFolder ) ;
@@ -1081,7 +1111,8 @@ private Dictionary<string, int> CreateDataTypeFolderStructure(IEnumerable<XEleme
1081
1111
for ( var i = 1 ; i < folders . Length ; i ++ )
1082
1112
{
1083
1113
var folderName = WebUtility . UrlDecode ( folders [ i ] ) ;
1084
- current = CreateDataTypeChildFolder ( folderName , current ) ;
1114
+ Guid ? folderKey = ( folderKeys . Length == folders . Length ) ? folderKeys [ i ] : null ;
1115
+ current = CreateDataTypeChildFolder ( folderName , folderKey ?? Guid . NewGuid ( ) , current ) ;
1085
1116
importedFolders [ name ] = current . Id ;
1086
1117
}
1087
1118
}
@@ -1090,17 +1121,17 @@ private Dictionary<string, int> CreateDataTypeFolderStructure(IEnumerable<XEleme
1090
1121
return importedFolders ;
1091
1122
}
1092
1123
1093
- private EntityContainer CreateDataTypeChildFolder ( string folderName , IUmbracoEntity current )
1124
+ private EntityContainer CreateDataTypeChildFolder ( string folderName , Guid folderKey , IUmbracoEntity current )
1094
1125
{
1095
1126
var children = _entityService . GetChildren ( current . Id ) . ToArray ( ) ;
1096
- var found = children . Any ( x => x . Name . InvariantEquals ( folderName ) ) ;
1127
+ var found = children . Any ( x => x . Name . InvariantEquals ( folderName ) || x . Key . Equals ( folderKey ) ) ;
1097
1128
if ( found )
1098
1129
{
1099
1130
var containerId = children . Single ( x => x . Name . InvariantEquals ( folderName ) ) . Id ;
1100
1131
return _dataTypeService . GetContainer ( containerId ) ;
1101
1132
}
1102
1133
1103
- var tryCreateFolder = _dataTypeService . CreateContainer ( current . Id , folderName ) ;
1134
+ var tryCreateFolder = _dataTypeService . CreateContainer ( current . Id , folderKey , folderName ) ;
1104
1135
if ( tryCreateFolder == false )
1105
1136
{
1106
1137
_logger . LogError ( tryCreateFolder . Exception , "Could not create folder: {FolderName}" , folderName ) ;
0 commit comments