5

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?

1 Answer 1

2

OK I've written a solution to my problem. See code below:

    public void CheckForComponentLinks(string sgId)
    {
        using (var client = new CoreServiceClient("basicHttp"))
        {
            // Read structure group details
            var structureGroup = (StructureGroupData)client.Read(sgId, new ReadOptions());

            // Parse metadata to Xml
            var metaData = structureGroup.Metadata;
            XNamespace xlink = "http://www.w3.org/1999/xlink";
            var elem = XElement.Parse(metaData);

            // Loop over each tcm id and start check
            foreach (var item in elem.Elements().Where(x => x.Attribute(xlink + "href") != null))
            {
               // Do something here
            }
        }
    }

So I'm parsing the Xml and checking for Tcm Ids, works a treat for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.