|
9 | 9 |
|
10 | 10 | <script language="C#" runat="server">
|
11 | 11 |
|
12 |
| - /// <summary> |
13 |
| - /// GET Parameters |
14 |
| - /// |
15 |
| - /// database: The Sitecore database. Example: web |
16 |
| - /// |
17 |
| - /// itemId: The dynamic placeholder migration is only processed on this start item. Example: {456B38B8-1C42-48AF-858E-FC58A2FC1491} |
18 |
| - /// |
19 |
| - /// enableRecursion: This option enables recursion for the start item. Example: true |
20 |
| - /// </summary> |
| 12 | + /// <summary> |
| 13 | + /// This script migrates the "old" dynamic placeholder format to the new format used in Sitecore 9. |
| 14 | + /// |
| 15 | + /// GET Parameters |
| 16 | + /// - database: The Sitecore database the script should run on. Example: master |
| 17 | + /// - itemId: The dynamic placeholder migration is only processed on this start item. Example: {456B38B8-1C42-48AF-858E-FC58A2FC1491} |
| 18 | + /// - enableRecursion: This option enables recursion for the start item. Example: true |
| 19 | + /// - allVersions: This option enables the migration of all versions of an item. Example: true |
| 20 | + /// </summary> |
21 | 21 |
|
22 | 22 | protected void Page_Load(object sender, EventArgs e)
|
23 | 23 | {
|
|
62 | 62 | return;
|
63 | 63 | }
|
64 | 64 |
|
65 |
| - bool isRecursionEnabled; |
66 |
| - bool.TryParse(Context.Request.QueryString["enableRecursion"], out isRecursionEnabled); |
| 65 | + bool isRecursionEnabled; |
| 66 | + bool migrateAllVersions; |
| 67 | + bool.TryParse(Context.Request.QueryString["enableRecursion"], out isRecursionEnabled); |
| 68 | + bool.TryParse(Context.Request.QueryString["allVersions"], out migrateAllVersions); |
67 | 69 |
|
68 | 70 | var fixRenderings = new UpgradeDynamicPlaceholderHelper(database);
|
69 | 71 |
|
70 | 72 | Sitecore.Diagnostics.Log.Info("Dynamic Placeholder Migration: Started", this);
|
71 | 73 |
|
72 |
| - var result = fixRenderings.Iterate(startItem, isRecursionEnabled); |
| 74 | + var result = fixRenderings.Iterate(startItem, isRecursionEnabled, migrateAllVersions); |
73 | 75 |
|
74 | 76 | OutputResult(result);
|
75 | 77 |
|
|
108 | 110 | _database = database;
|
109 | 111 | }
|
110 | 112 |
|
111 |
| - public Dictionary<Item, List<KeyValuePair<string, string>>> Iterate(Item startItem, bool isRecursionEnabled) |
| 113 | + public Dictionary<Item, List<KeyValuePair<string, string>>> Iterate(Item startItem, bool isRecursionEnabled, bool migrateAllVersions) |
112 | 114 | {
|
113 | 115 | var result = new Dictionary<Item, List<KeyValuePair<string, string>>>();
|
114 | 116 |
|
115 | 117 | var items = new List<Item>();
|
116 | 118 |
|
117 | 119 | if (startItem != null)
|
118 | 120 | {
|
| 121 | + items.Add(startItem); |
| 122 | +
|
119 | 123 | if (isRecursionEnabled)
|
120 | 124 | {
|
121 | 125 | items.AddRange(_database.SelectItems(string.Format(ItemsWithPresentationDetailsQuery, startItem.Paths.FullPath)));
|
122 | 126 | }
|
123 |
| - else |
124 |
| - { |
125 |
| - items.Add(startItem); |
126 |
| - } |
127 | 127 | }
|
128 | 128 | else
|
129 | 129 | {
|
130 | 130 | items.AddRange(_database.SelectItems(string.Format(ItemsWithPresentationDetailsQuery, "/sitecore/content")));
|
131 | 131 | }
|
132 | 132 |
|
133 |
| - var layoutFieldIds = new[] {FieldIDs.LayoutField, FieldIDs.FinalLayoutField}; |
134 |
| -
|
135 | 133 | foreach (var itemInDefaultLanguage in items)
|
136 | 134 | {
|
137 | 135 | foreach (var itemLanguage in itemInDefaultLanguage.Languages)
|
138 | 136 | {
|
139 | 137 | var item = itemInDefaultLanguage.Database.GetItem(itemInDefaultLanguage.ID, itemLanguage);
|
140 | 138 | if (item.Versions.Count > 0)
|
141 | 139 | {
|
142 |
| - foreach (var layoutFieldId in layoutFieldIds) |
143 |
| - { |
144 |
| - var layoutField = item.Fields[layoutFieldId]; |
| 140 | + ChangeLayout(result, item, FieldIDs.LayoutField, false); |
| 141 | + ChangeLayout(result, item, FieldIDs.FinalLayoutField, true); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
145 | 145 |
|
146 |
| - // Don't convert standard values! |
147 |
| - if (layoutField.ContainsStandardValue) |
148 |
| - { |
149 |
| - continue; |
150 |
| - } |
| 146 | + return result; |
| 147 | + } |
151 | 148 |
|
152 |
| - var changeResult = ChangeLayoutFieldForItem(item, layoutField); |
| 149 | + private void ChangeLayout(Dictionary<Item, List<KeyValuePair<string, string>>> result, Item item, ID layoutFieldId, bool migrateAllVersions) |
| 150 | + { |
| 151 | + if (migrateAllVersions) |
| 152 | + { |
| 153 | + var versionCount = item.Versions.GetVersionNumbers().Length; |
| 154 | + var versions = item.Versions.GetVersions(false); |
153 | 155 |
|
154 |
| - if (changeResult.Any()) |
155 |
| - { |
156 |
| - if (!result.ContainsKey(item)) |
157 |
| - { |
158 |
| - result.Add(item, changeResult); |
159 |
| - } |
160 |
| - else |
161 |
| - { |
162 |
| - result[item].AddRange(changeResult); |
163 |
| - } |
164 |
| - } |
165 |
| - } |
166 |
| - } |
| 156 | + if (versions.Length != versionCount) |
| 157 | + { |
| 158 | + Sitecore.Diagnostics.Log.Warn(string.Format("MigrateDynamicPlaceholders: {0} - Could not load all item versions", item.Name), this); |
| 159 | + } |
| 160 | +
|
| 161 | + foreach (var itemVersion in versions) |
| 162 | + { |
| 163 | + ChangeLayoutInternal(result, itemVersion, layoutFieldId); |
167 | 164 | }
|
168 | 165 | }
|
| 166 | + else |
| 167 | + { |
| 168 | + ChangeLayoutInternal(result, item, layoutFieldId); |
| 169 | + } |
| 170 | + } |
169 | 171 |
|
170 |
| - return result; |
| 172 | + private void ChangeLayoutInternal(Dictionary<Item, List<KeyValuePair<string, string>>> result, Item item, ID layoutFieldId) |
| 173 | + { |
| 174 | + var layoutField = item.Fields[layoutFieldId]; |
| 175 | +
|
| 176 | + // Don't convert standard values! |
| 177 | + if (layoutField.ContainsStandardValue) |
| 178 | + { |
| 179 | + return; |
| 180 | + } |
| 181 | +
|
| 182 | + var changeResult = ChangeLayoutFieldForItem(item, layoutField); |
| 183 | +
|
| 184 | + if (changeResult.Any()) |
| 185 | + { |
| 186 | + if (!result.ContainsKey(item)) |
| 187 | + { |
| 188 | + result.Add(item, changeResult); |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + result[item].AddRange(changeResult); |
| 193 | + } |
| 194 | + } |
171 | 195 | }
|
172 | 196 |
|
173 | 197 | private List<KeyValuePair<string, string>> ChangeLayoutFieldForItem(Item currentItem, Field field)
|
|
181 | 205 | var details = LayoutDefinition.Parse(xml);
|
182 | 206 |
|
183 | 207 | var device = details.GetDevice(DefaultDeviceId);
|
184 |
| -
|
| 208 | + |
185 | 209 | if (device != null && device.Renderings != null)
|
186 | 210 | {
|
187 | 211 | bool requiresUpdate = false;
|
|
0 commit comments