Created
September 1, 2022 13:26
-
-
Save jammykam/1970c78de2ad4842bf557862991b2ac1 to your computer and use it in GitHub Desktop.
This script migrates the "old" layout format to the new format used in Sitecore 8.2 Update-7 and Sitecore 9 Update-2. Code from: https://sitecore.namics.com/upgrading-to-sitecore-9-0-update-2-and-later-versions-might-change-the-order-of-renderings/ updated to use ID rather than path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" %> | |
<%@ Import Namespace="System.Linq" %> | |
<%@ Import Namespace="Sitecore" %> | |
<%@ Import Namespace="Sitecore.Configuration" %> | |
<%@ Import Namespace="Sitecore.Data" %> | |
<%@ Import Namespace="Sitecore.Data.Fields" %> | |
<%@ Import Namespace="Sitecore.Data.Items" %> | |
<%@ Import Namespace="Sitecore.Globalization" %> | |
<%@ Import Namespace="Sitecore.Layouts" %> | |
<script language="C#" runat="server"> | |
/// https://sitecore.namics.com/upgrading-to-sitecore-9-0-update-2-and-later-versions-might-change-the-order-of-renderings/ | |
/// <summary> | |
/// This script migrates the "old" layout format to the new format used in Sitecore 8.2 Update-7 and Sitecore 9 Update-2. | |
/// Sitecore Knowledge Base article: https://kb.sitecore.net/articles/672981 | |
/// | |
/// GET Parameters | |
/// - database: The Sitecore database the script should run on. Example: master | |
/// - itemId: The dynamic placeholder migration is only processed on this start item. Example: {456B38B8-1C42-48AF-858E-FC58A2FC1491} | |
/// - enableRecursion: This option enables recursion for the start item. Example: true | |
/// </summary> | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
if (!Sitecore.Context.User.IsAdministrator) | |
{ | |
Response.Write("You have no permission to run this script."); | |
return; | |
} | |
var databaseName = Context.Request.QueryString["database"]; | |
if (string.IsNullOrEmpty(databaseName)) | |
{ | |
databaseName = "master"; | |
} | |
var database = Factory.GetDatabase(databaseName); | |
ID startItemId = null; | |
Item startItem = null; | |
var startItemIdString = Context.Request.QueryString["itemId"]; | |
if (string.IsNullOrEmpty(startItemIdString)) | |
{ | |
Response.Write("No Item ID provided. Please use GET parameter 'itemId'."); | |
return; | |
} | |
if (!string.IsNullOrEmpty(startItemIdString) && | |
!Sitecore.Data.ID.TryParse(startItemIdString, out startItemId)) | |
{ | |
Response.Write("Invalid Item ID."); | |
return; | |
} | |
if (!string.IsNullOrEmpty(startItemIdString)) | |
{ | |
startItem = database.GetItem(startItemId); | |
} | |
if (!string.IsNullOrEmpty(startItemIdString) && startItem == null) | |
{ | |
Response.Write(string.Format("No item found for ID {0}.", startItemIdString)); | |
return; | |
} | |
bool isRecursionEnabled; | |
bool.TryParse(Context.Request.QueryString["enableRecursion"], out isRecursionEnabled); | |
var fixRenderings = new UpgradeLayoutHelper(database); | |
Sitecore.Diagnostics.Log.Info("Layout Migration: Started", this); | |
fixRenderings.Iterate(startItem, isRecursionEnabled); | |
Sitecore.Diagnostics.Log.Info("Layout Migration: Finished", this); | |
} | |
public class UpgradeLayoutHelper | |
{ | |
private const string ItemsWithPresentationDetailsQuery = | |
"{0}//*[@__Renderings != '' or @__Final Renderings != '']"; | |
private readonly Database _database; | |
public UpgradeLayoutHelper(Database database) | |
{ | |
_database = database; | |
} | |
public Dictionary<Item, List<KeyValuePair<string, string>>> Iterate(Item startItem, bool isRecursionEnabled) | |
{ | |
var result = new Dictionary<Item, List<KeyValuePair<string, string>>>(); | |
var items = new List<Item>(); | |
if (startItem != null) | |
{ | |
items.Add(startItem); | |
if (isRecursionEnabled) | |
{ | |
items.AddRange(_database.SelectItems(string.Format(ItemsWithPresentationDetailsQuery, | |
startItem.ID))); | |
} | |
} | |
else | |
{ | |
items.AddRange( | |
_database.SelectItems(string.Format(ItemsWithPresentationDetailsQuery, "/sitecore/content"))); | |
} | |
foreach (var itemInDefaultLanguage in items) | |
{ | |
UpdateLayoutField(itemInDefaultLanguage); | |
} | |
return result; | |
} | |
public void UpdateLayoutField(Item item) | |
{ | |
bool isSharedLayoutFieldUpdated = false; | |
foreach (var language in item.Languages) | |
{ | |
Item itemInLanguage = _database.GetItem(item.ID, language); | |
if (itemInLanguage.Versions.Count > 0) | |
{ | |
foreach (Item itemVersion in itemInLanguage.Versions.GetVersions()) | |
{ | |
foreach (Field f in itemVersion.Fields) | |
{ | |
if (f.ID == FieldIDs.FinalLayoutField) | |
{ | |
itemVersion.Editing.BeginEdit(); | |
string fieldValue = Sitecore.Data.Fields.LayoutField.GetFieldValue(itemVersion.Fields[FieldIDs.FinalLayoutField]); | |
LayoutField.SetFieldValue(f, fieldValue); | |
itemVersion.Editing.EndEdit(); | |
} | |
} | |
if (!isSharedLayoutFieldUpdated) | |
{ | |
foreach (Field f in itemVersion.Fields) | |
{ | |
if (f.ID == FieldIDs.LayoutField) | |
{ | |
itemVersion.Editing.BeginEdit(); | |
string fieldValue = LayoutField.GetFieldValue(itemVersion.Fields[FieldIDs.LayoutField]); | |
LayoutField.SetFieldValue(f, fieldValue); | |
itemVersion.Editing.EndEdit(); | |
isSharedLayoutFieldUpdated = true; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
</script> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<title></title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<div> | |
Done. | |
</div> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment