I have a component that is associated with many thousands of elements (in this case, all other elements are components). Is there an easy / quick way to remove a component and remove all links?
I am currently using Tridion 5.3 and doing this programmatically through the TOM API. For one component that was associated with 10,000 other components, this took about 7 hours. I need a lot more!
Now I'm moving from R5.3 in 2011, so I can use it either for the task.
The code I use is as follows:
static void Main(string[] args) { var componentIDToRemove = "tcm:4-123456"; var linkedComponentIDs = System.IO.File.ReadAllLines("C:\\...\\whereused.txt"); // ids of the components linked to tcm:4-123456 TDS.TDSE tdse = new TDS.TDSE(); foreach (var linkedComponentID in linkedComponentIDs) { TDS.Component component = null; TDS.ItemFieldValues itemFieldValues = null; try { component = (TDS.Component)tdse.GetObject(linkedComponentID, TDSDefines.EnumOpenMode.OpenModeView); itemFieldValues = component.MetadataFields["myfield"].value; var itemFieldValuesCount = itemFieldValues.Count; for (var i = itemFieldValuesCount; i > 0; i--) { if (itemFieldValues[i].ID == componentIDToRemove) { component.CheckOut(); itemFieldValues.Remove(i); component.Save(); component.CheckIn(); } } } finally { // release the TDS objects from memory ReleaseObject(component); ReleaseObject(itemFieldValues); } } } public static void ReleaseObject(object o) { try { if (o != null) { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o); } } finally { GC.Collect(); GC.WaitForPendingFinalizers(); } }
source share