Skip to content

Instantly share code, notes, and snippets.

@karbyninc
Last active February 11, 2016 17:08
Show Gist options
  • Save karbyninc/e3e069415751f5ca6d21 to your computer and use it in GitHub Desktop.
Save karbyninc/e3e069415751f5ca6d21 to your computer and use it in GitHub Desktop.
Sitecore Tip: Updating Datasources After Upgrading to Sitecore 7
///
/// Updates the datasource for a rendering from an item path to using the
/// Sitecore ID for the item. The benefit is that this will allow datasources
/// to be able to be freely moved from one area of the content tree to another
/// while enabling the rendering to still function as expected.
///
///The Sitecore.Data.Items.Item to update the datasources for.
public void UpdateDataSourcePathToId(Sitecore.Data.Items.Item itemToProcess)
{
if (null != itemToProcess)
{
// First we need to get the LayoutDefinition from the item using the predefined constant Sitecore.FieldIDs.LayoutField
LayoutDefinition layout = LayoutDefinition.Parse(itemToProcess[Sitecore.FieldIDs.LayoutField]);
if (layout != null)
{
// We want to go through all of the devices for the layout
foreach (DeviceDefinition device in layout.Devices)
{
if (null != device.Renderings)
{
foreach (RenderingDefinition definition in device.Renderings)
{
// Since we want to change paths to IDs, we make sure we have an existing
// datasource and that it is not already a Sitecore ID
if (!string.IsNullOrEmpty(definition.Datasource)
& amp; & !Sitecore.Data.ID.IsID(definition.Datasource))
{
// Check to see if we can get the item, we are assuming a path to an item and
// not a query
Item datasourceItem = Sitecore.Context.Database.Items.GetItem(definition.Datasource);
if (null != datasourceItem)
{
RenderingUtil.UpdateRenderingDatasource(layout, definition, itemToProcess, datasourceItem.ID.ToString());
}
}
}
}
}
}
}
}
///
/// Updates a datasource for a specific item in Sitecore.
///
///The Sitecore.Layouts.LayoutDefinition used by the item.
///The current Sitecore.Layouts.RenderingDefinition being processed for which the datasource will be updated.
///The Sitecore.Data.Items.Item to process.
///The new value to use for the datasource represented as a string.
public static void UpdateRenderingDatasource(Sitecore.Layouts.LayoutDefinition layout, Sitecore.Layouts.RenderingDefinition rendering, Sitecore.Data.Items.Item itemToUpdate, string newDatasourceValue)
{
if (null != rendering & amp; & null != itemToUpdate
& amp; & !string.IsNullOrEmpty(newDatasourceValue))
{
// We store this to use in logging
string originalDatasourceValue = rendering.Datasource;
// We only want to update values that have actually changed
if (!originalDatasourceValue.Equals(newDatasourceValue, StringComparison.OrdinalIgnoreCase))
{
// We need to change the datasource and get the full XML for the layout in order to perform the update
rendering.Datasource = newDatasourceValue;
string updatedLayoutInformation = layout.ToXml();
using (new Sitecore.SecurityModel.SecurityDisabler())
{
// We need to disable security since it may be run outside of admin context and begin editing the item
itemToUpdate.Editing.BeginEdit();
try
{
itemToUpdate.Fields[Sitecore.FieldIDs.LayoutField].Value = updatedLayoutInformation;
itemToUpdate.Editing.EndEdit();
Sitecore.Diagnostics.Log.Info(string.Format("UpdateRenderingDatasource: Datasource updated on item: {0} from {1} to {2}", itemToUpdate.Paths.FullPath, originalDatasourceValue, newDatasourceValue), typeof(RenderingUtil));
}
catch (Exception xcp)
{
itemToUpdate.Editing.CancelEdit();
Sitecore.Diagnostics.Log.Error("UpdateRenderingDatasource: Failed to update datasource.", xcp, typeof(RenderingUtil));
}
}
}
else
{
Sitecore.Diagnostics.Log.Warn("UpdateRenderingDatasource: Datasource value is unchanged.", typeof(RenderingUtil));
}
}
else
{
Sitecore.Diagnostics.Log.Warn("UpdateRenderingDatasource: Unable to process datasource change. The item to update or rendering is null or a new datasource value has not been specified.", typeof(RenderingUtil));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment