Skip to content

Instantly share code, notes, and snippets.

@Azadehkhojandi
Created April 12, 2016 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Azadehkhojandi/67af50f3f6e5888eed749ca68dfcd01a to your computer and use it in GitHub Desktop.
Save Azadehkhojandi/67af50f3f6e5888eed749ca68dfcd01a to your computer and use it in GitHub Desktop.
Migrating cloned items to sitcore 8.1 and above
<%@ page language="C#" async="true" autoeventwireup="true" codebehind="FixClonedItems.aspx.cs" inherits="Web.sitecore.admin.Migration.FixClonedItems" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Migartion Fix Cloned Items</title>
<style type="text/css">
body {
font-family: normal 11pt "Times New Roman", Serif;
}
</style>
</head>
<body>
<form runat="server" id="form">
<p>update __Source Item value of cloned items in Retail store directory </p>
<asp:button runat="server" id="BtnUpdateItems" onclick="UpdateItemsOnClick" text="Update Items" />
<br/>
<asp:literal runat="server" id="litStatus" text="status..."></asp:literal>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.ContentSearch.Maintenance;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.sitecore.admin;
using SC = Sitecore;
namespace Web.sitecore.admin.Migration
{
public partial class FixClonedItems : AdminPage
{
protected void Page_Load(object sender, EventArgs e)
{
CheckSecurity();
}
private void PauseIndexing()
{
IndexCustodian.PauseIndexing();
SC.Configuration.Settings.Indexing.Enabled = false;
}
private void ResumeIndexing()
{
IndexCustodian.ResumeIndexing();
SC.Configuration.Settings.Indexing.Enabled = true;
}
protected async void UpdateItemsOnClick(object sender, EventArgs e)
{
BtnUpdateItems.Enabled = false;
await UpdateItems("/sitecore/content/");
}
private Database GetDb()
{
return Database.GetDatabase("master");
}
public async Task UpdateItems(string path)
{
litStatus.Text = "starting.....";
PauseIndexing();
var db = GetDb();
if (db != null)
{
var item = db.GetItem(path);
if (item != null && item.HasChildren)
{
var tasks = new List<Task<string>>();
tasks.Add(UpdateAsync(new[] { item }));
var children = item.Children.ToList();
var pagesize = children.Count / 10;
for (int i = 0; i <= 10; i++)
{
tasks.Add(UpdateAsync(children.Skip(pagesize * i).Take(pagesize).ToList()));
}
await Task.WhenAll(tasks);
var message = new StringBuilder();
foreach (var task in tasks)
{
message.Append("<br/>" + task.Result);
}
litStatus.Text = message.ToString();
}
else
{
litStatus.Text = "item is null or item doesn't have any children";
}
}
else
{
litStatus.Text = "error: couldn't find master db";
}
ResumeIndexing();
}
private Task<string> UpdateAsync(ICollection<Item> items)
{
return Task<string>.Factory.StartNew(() => Update(items));
}
private string Update(ICollection<Item> items)
{
if (items != null && items.Any())
{
var sp = new Stopwatch();
sp.Start();
using (new SC.SecurityModel.SecurityDisabler())
{
foreach (var store in items)
{
if (store.HasClones)
{
var uri = store.Paths.Item.Uri.ToString();
var qloc = uri.IndexOf("?", StringComparison.InvariantCultureIgnoreCase);
var sourceItem = store.Paths.Item.Uri.ToString()
.Substring(0, qloc > 0 ? qloc : uri.Length);
foreach (var clonedStore in store.GetClones())
{
try
{
using (new EditContext(clonedStore))
{
clonedStore["__Source Item"] = sourceItem;
//clonedStore["ClonedItem"] = store.ID.ToString();
}
}
catch (Exception ex)
{
//kill exception
SC.Diagnostics.Log.Error(clonedStore.ID + ex.Message, ex, this);
}
}
}
}
return $"Processed Items:{items.Count} Elapsed: {sp.Elapsed}";
}
}
return string.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment