Skip to content

Instantly share code, notes, and snippets.

@rcurlette
Created May 24, 2018 08:11
Show Gist options
  • Save rcurlette/4a07068a7a8abdf2c8508e8a0fb0140f to your computer and use it in GitHub Desktop.
Save rcurlette/4a07068a7a8abdf2c8508e8a0fb0140f to your computer and use it in GitHub Desktop.
Tridion Update Multimedia Component Metadata XML using Core Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Tridion.ContentManager.CoreService.Client;
namespace FixMMCompMetadataField
{
/// <summary>
/// App to Update or remove Metadata fields of a Multimedia Component.
/// Background: Somehow the Metadata of the MM Component had become corrupt
/// with a Category field referencing a non-existing keyword. This caused
/// publishing issues with DXA, as it attempts to render the fields of the
/// Component and fails.
///
/// Thanks for the help from the following StackOverflow post:
/// https://tridion.stackexchange.com/questions/11597/how-to-update-the-component-particular-field-node-using-core-service?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
/// </summary>
public class Program
{
static string binding = "netTcp_201501";
static void Main(string[] args)
{
string compUri = "tcm:4-123";
List<FieldModel> fields = new List<FieldModel>()
{
new FieldModel()
{
name = "metadataFieldName1",
value=""
},
new FieldModel()
{
name = "metadataFieldName2",
value="Promoting Health"
}
};
// bool success = UpdateFields(compUri,fields); // multiple-fields
bool success = RemoveFields(compUri, fields); // remove multiple-fields
}
public static bool RemoveFields(string componentUri, List<FieldModel> fields)
{
bool success = true;
using (SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient(binding))
{
var component = (ComponentData)client.TryCheckOut(componentUri, null);
try
{
if (component.IsEditable.Value)
{
XElement xmlData;
XDocument doc;
doc = XDocument.Parse(component.Metadata);
xmlData = doc.Root;
var ns = xmlData.GetDefaultNamespace();
foreach (var field in fields)
{
if (xmlData.Descendants(ns + field.name).Any())
{
XElement itemToRemove = xmlData.Descendants(ns + field.name).SingleOrDefault();
itemToRemove.Remove();
//xmlData.Descendants(ns + field.name).SingleOrDefault().Value = field.value;
component.Metadata = doc.ToString();
}
else
{
// field not found
}
}
client.Update(component, null);
}
else
{
throw new Exception(string.Format("The Item '{0}' is checked out by some other user, can't be updated",
component.Title));
}
}
catch (Exception ex)
{
client.UndoCheckOut(componentUri, false, null);
// todo - log
success = false;
throw;
}
}
return success;
}
public static bool UpdateFields(string componentUri, List<FieldModel> fields)
{
bool success = true;
using (SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient(binding))
{
var component = (ComponentData)client.TryCheckOut(componentUri, null);
try
{
if (component.IsEditable.Value)
{
XElement xmlData;
XDocument doc;
doc = XDocument.Parse(component.Metadata);
xmlData = doc.Root;
var ns = xmlData.GetDefaultNamespace();
foreach (var field in fields)
{
if (xmlData.Descendants(ns + field.name).Any())
{
xmlData.Descendants(ns + field.name).SingleOrDefault().Value = field.value;
component.Metadata = doc.ToString();
}
else
{
// field not found
}
}
client.Update(component, null);
}
else
{
throw new Exception(string.Format("The Item '{0}' is checked out by some other user, can't be updated",
component.Title));
}
}
catch (Exception ex)
{
client.UndoCheckOut(componentUri, false, null);
// todo - log
success = false;
throw;
}
}
return success;
}
}
public class FieldModel
{
public string name { get; set; }
public string value { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment