Skip to content

Instantly share code, notes, and snippets.

@gerhart92
Created October 2, 2021 23:56
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 gerhart92/59bf86945aca587d270f490ff5c89e89 to your computer and use it in GitHub Desktop.
Save gerhart92/59bf86945aca587d270f490ff5c89e89 to your computer and use it in GitHub Desktop.
namespace Feature.Serialization.Services
{
using System;
using Sitecore.Data.Items;
using Sitecore.Abstractions.Serialization;
using Sitecore.Data.Serialization;
/// <summary>
/// Serialization service for item serialization manager
/// </summary>
public class ItemSerializationService : IItemSerializationService
{
private readonly BaseItemSerializationManager _itemSerializationManager;
/// <summary>
/// Constructor for service
/// </summary>
/// <param name="itemSerializationManager">Item serialization manager</param>
public ItemSerializationService(BaseItemSerializationManager itemSerializationManager)
{
_itemSerializationManager = itemSerializationManager;
}
/// <summary>
/// Method which dumps the item to a corresponding file under dump root
/// This will replace the following method from obsolete Manager: Sitecore.Data.Serialization.Manager.DumpItem(item)
/// </summary>
public void DumpItem(Item item)
{
try
{
_itemSerializationManager.DumpItem(item);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error dumping the item: " + item.DisplayName, ex, this);
}
}
/// <summary>
/// Method which dumps the tree under the dump root.
/// This will replace the following method from obsolete Manager: Sitecore.Data.Serialization.Manager.DumpTree(item)
/// </summary>
public void DumpTree(Item item)
{
try
{
_itemSerializationManager.DumpTree(item);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error dumping the tree: " + item.DisplayName, ex, this);
}
}
/// <summary>
/// Method which loads the specified Item
/// This will replace the following method from obsolete Manager: Sitecore.Data.Serialization.Manager.LoadItem(item, loadOptions)
/// </summary>
public Item LoadItem(Item item, LoadOptions loadOptions)
{
try
{
return _itemSerializationManager.LoadItem(item, loadOptions);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error loading the item: " + item.DisplayName, ex, this);
return null;
}
}
/// <summary>
/// Method which loads the item from the specified file.
/// </summary>
public Item LoadItemByPath(string path, LoadOptions loadOptions)
{
try
{
return _itemSerializationManager.LoadItem(path, loadOptions);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error loading item by path: " + path, ex, this);
return null;
}
}
/// <summary>
/// Method which loads the subtree from specified path.
/// This will replace the following method from obsolete Manager: Sitecore.Data.Serialization.Manager.LoadTree(path, loadOptions)
/// </summary>
public void LoadItemByPath(Item rootItem, LoadOptions loadOptions)
{
try
{
_itemSerializationManager.LoadTree(rootItem, loadOptions);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error loading item by path: " + rootItem.DisplayName, ex, this);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment