Skip to content

Instantly share code, notes, and snippets.

@bill-barron
Last active August 29, 2015 14:02
Show Gist options
  • Save bill-barron/6f69c700b3d9f948dcdb to your computer and use it in GitHub Desktop.
Save bill-barron/6f69c700b3d9f948dcdb to your computer and use it in GitHub Desktop.
Helper class for reading multiple fields of data from Sitecore items. Also for transferring multiple fields of data between items with different field names
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Collections;
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Security;
using Sitecore.SecurityModel;
public class TransferData {
/// <summary>
/// Checks to make sure the given item has all the listed fields
/// </summary>
/// <param name="item">The given item</param>
/// <param name="fieldNames">the names of all the fields to check</param>
/// <returns>True if the item has all the given fields and false otherwise.</returns>
public static bool HasFields(Item item, List<string> fieldNames)
{
if (item == null)
return false;
// Check all fields before transferring anything
foreach (string fieldName in fieldNames)
if (item.Fields[fieldName] == null)
return false;
return true;
}
/// <summary>
/// Read a list of fields from a Sitecore Item to a Dictionary, with the value equal to the field value and the key determined by the fieldMap.
/// Allows you to read the "Title" field, but map the output to some other key such as "Heading"
/// </summary>
/// <param name="source">The Source Item</param>
/// <param name="fieldMap">A mapping of input field names to output keys</param>
/// <param name="allOrNothing">return an empty result if the given item doesn't have all the listed fields</param>
/// <returns>All the read values or an empty dictionary</returns>
public static Dictionary<string, string> ReadFieldValuesAs(Item source, Dictionary<string, string> fieldMap, bool allOrNothing)
{
Dictionary<string, string> result = new Dictionary<string, string>();
List<string> fieldNames = fieldMap.Keys.ToList<string>();
if (allOrNothing && !HasFields(source, fieldNames))
return result;
foreach (string fieldName in fieldNames)
result.Add(fieldMap[fieldName], (source.Fields[fieldName] == null) ? null : source.Fields[fieldName].Value);
return result;
}
/// <summary>
/// Write values to multiple fields of a Sitecore Item. Null values are written as the empty string.
/// </summary>
/// <param name="dest">The destination item to write to</param>
/// <param name="data">The field names (keys) to write to and values to write</param>
/// <param name="allOrNothing">Ensure all the fields exist before making any changes</param>
/// <returns>True if successful, false otherwise</returns>
public static bool WriteFieldValues(Item dest, Dictionary<string, string> data, bool allOrNothing)
{
List<string> fieldNames = data.Keys.ToList<string>();
if (allOrNothing && !HasFields(dest, fieldNames))
return false;
using (new SecurityDisabler())
using (new EditContext(dest))
foreach (string fieldName in fieldNames)
if (dest.Fields[fieldName] != null)
dest.Fields[fieldName].Value = (data[fieldName] == null) ? string.Empty : data[fieldName];
return true;
}
/// <summary>
/// Transfer data from one item to another using the given map of source field names to destination field names
/// </summary>
/// <param name="source">The source item containing the data to transfer</param>
/// <param name="dest">The destination item that receives the data</param>
/// <param name="fieldMap">A mapping of source field namess to destination field names. Key = source field name, Value = Destinatino field name</param>
/// <returns>True if everything was transferred, False if it was not transferred</returns>
public static bool CopyFieldValues(Item source, Item dest, Dictionary<string, string> fieldMap, bool allOrNothing)
{
if (source == null || dest == null)
return false;
Dictionary<string, string> data = ReadFieldValuesAs(source, fieldMap, allOrNothing);
if (data == null)
return false;
return WriteFieldValues(dest, data, allOrNothing);
}
}
public class Example
{
//Example 1: Copy Title, Body and Date from oldItem into fields Heading, Body and Release Date of newItem:
public static void Example1()
{
TransferData.CopyFieldValues(source: oldItem, dest: newItem, fieldMap: new Dictionary<string, string>() {
{ "Title", "Heading" },
{ "Body", "Body" },
{ "Date", "Release Date" }
}, allOrNothing: true);
}
//Example 2: Do the same as above then also write "1" to the "Visible" field
public static void Example2()
{
var oldData = TransferData.ReadFieldsAs(source: oldItem, fieldMap: new Dictionary<string, string>() {
{ "Title", "Heading" },
{ "Body", "Body" },
{ "Date", "Release Date" }
}, allOrNothing: true);
oldData.Add("Visible", "1");
TransferData.WriteFieldValues(dest: newItem, data: oldData, allOrNothing: true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment