Created
March 3, 2017 18:50
-
-
Save pmedcraft/91baef7bb6a68cc8beac06efec131a32 to your computer and use it in GitHub Desktop.
C# code to replace the relationship between a provided SDL Web (Tridion) component and all its "where using" components with a reference (stub) to an asset originating from SDL Media Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void UpdateWhereUsingComponents(ICoreService coreServiceClient, SessionAwareEclServiceClient eclClient, ComponentData componentData, Dictionary<string, object> uploadInfo) | |
{ | |
Dictionary<string, string> stubInfo = null; | |
try | |
{ | |
stubInfo = eclClient.CreateOrGetStubUris(new List<string> { "ecl:5-mm-" + uploadInfo["distributionId"] + "-dist-file" }); | |
} | |
catch(Exception exception) | |
{ | |
log.Error("Error generating stub for image " + componentData.Id); | |
log.Error(exception); | |
} | |
if (stubInfo == null || stubInfo.Count == 0) | |
return; | |
log.Info("Stub " + stubInfo.Values.First() + " generated for " + componentData.Id); | |
// Create a filter | |
UsingItemsFilterData usingItemsFilterData = new UsingItemsFilterData | |
{ | |
BaseColumns = ListBaseColumns.Id, // to specify the detail in the XML | |
IncludedVersions = VersionCondition.OnlyLatestVersions, | |
ItemTypes = new[] { ItemType.Component } // to specify certain items | |
}; | |
SynchronizeOptions syncOptions = new SynchronizeOptions(); | |
syncOptions.SynchronizeFlags = SynchronizeFlags.FixNamespace | SynchronizeFlags.ApplyDefaultValuesForMissingMandatoryFields; | |
ReadOptions readOptions = new ReadOptions(); | |
// Get the XML by calling .GetListXml on the client | |
XElement usingXML = coreServiceClient.GetListXml(componentData.Id, usingItemsFilterData); | |
XNamespace df = usingXML.Name.Namespace; | |
IEnumerable<XElement> itemElements = usingXML.Elements(df + "Item"); | |
foreach (XElement itemElement in itemElements) | |
{ | |
string usingItemTcmid = itemElement.FirstAttribute.Value; | |
string usingItemPubId = usingItemTcmid.Split('-')[0]; | |
string multimediaLinkId = usingItemPubId + "-" + componentData.Id.Split('-')[1]; | |
string stubTcmId = usingItemPubId + "-" + stubInfo.Values.First().Split('-')[1]; | |
ComponentData usingItem = coreServiceClient.Read(usingItemTcmid, readOptions) as ComponentData; | |
usingItem = (ComponentData)coreServiceClient.SynchronizeWithSchema(usingItem, syncOptions).SynchronizedItem; | |
Boolean modified = false; | |
if (!String.IsNullOrEmpty(usingItem.Content) && usingItem.Content.Contains(multimediaLinkId)) | |
{ | |
usingItem.Content = usingItem.Content.Replace(multimediaLinkId, stubTcmId); | |
modified = true; | |
} | |
if (!String.IsNullOrEmpty(usingItem.Metadata) && usingItem.Metadata.Contains(multimediaLinkId)) | |
{ | |
usingItem.Metadata = usingItem.Metadata.Replace(multimediaLinkId, stubTcmId); | |
modified = true; | |
} | |
if (modified) | |
{ | |
try | |
{ | |
coreServiceClient.CheckOut(usingItem.Id, true, readOptions); | |
coreServiceClient.Save(usingItem, readOptions); | |
coreServiceClient.CheckIn(usingItem.Id, true, "Updated by the migration tool", readOptions); | |
log.Info("UPDATED RELATIONSHIP: " + usingItem.Id); | |
} | |
catch(Exception exception) | |
{ | |
log.Error("Unable to update relationship in Tridion!"); | |
log.Error(exception); | |
try | |
{ | |
coreServiceClient.UndoCheckOut(usingItem.Id, true, readOptions); | |
} | |
catch(Exception undoCheckOutException) | |
{ | |
log.Error("Unable to undo checkout!"); | |
log.Error(undoCheckOutException); | |
} | |
continue; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment