I'm trying to check for component links in structure groups so that I can re-create the linked components with the following code:
public void CheckForComponentLinks(string sgId)
{
using (var client = new CoreServiceClient("basicHttp"))
{
// Read structure group details
var structureGroup = (StructureGroupData)client.Read(sgId, new ReadOptions());
// Get schema fields
var schemaFields = client.ReadSchemaFields(structureGroup.MetadataSchema.IdRef, true, new ReadOptions());
// Loop over metadata and check for component links
foreach (var field in schemaFields.MetadataFields.OfType<ComponentLinkFieldDefinitionData>())
{
Console.WriteLine("Component Link");
}
Console.ReadLine();
}
}
I've managed to get my console to write out every time it finds a component link, but I need to now get the component data for each one found.
So ideally I'd like to be able to do something like this:
ComponentData cData = (ComponentData)client.Read(COMPONENT_ID, new ReadOptions());
But the problem is I can't get the "COMPONENT_ID" of the component from "field". The only thing I can think of is if I parse the XML that I get back from structureGroup.Metadata and check for tcm Ids?
I've also had a look at Frank' s Fields class but can't see how to get back the tcm Id from ForMetadataOf.
Does anyone know of a way I can achieve this?