3

I was wondering if there's a way before creating a component to check for component links either from the fields of the component or through RTF.

I'm using the following code that is creating a copy of existing components. The aim is to take components from one publication and copy them to another programmatically. Whilst doing this I need to check the contents of every component for component links and recreate them too

The code below isn't complete yet as I still need to change the location of the component to be that of the target publication but you will get the jist of where the component fields are coming from.

public string Create(CoreServiceClient client, string componentId)
{
var compData = (ComponentData)client.Read(componentId, new ReadOptions());

var component = (ComponentData)client.GetDefaultData(ItemType.Component, compData.LocationInfo.OrganizationalItem.IdRef);

component.Id = "tcm:0-0-0";
component.Title = compData.Title;
component.Content = compData.Content;
component.Schema.IdRef = compData.Schema.IdRef;

try
{
    component = (ComponentData)client.Create(component, new ReadOptions());
}
catch (Exception ex)
{
    Console.WriteLine("Unable to create component: " + ex);
}

return component.Id;
}

Is there a way I can target individual fields and get out tcm ids of component links?

The component links could come from either a RTF or individual fields in the component. So I guess I'd need to target the component.Component field and check for tcm ids here?

Many thanks

3
  • Your question is a bit misleading. You're asking about retrieving component links from existing components, yet showing a code sample for creating new components. Where will the values come from for the new components? Commented Apr 18, 2013 at 22:59
  • I am a little confused by your code. It looks like it is creating a copy of an existing component in the same folder. What are you trying to achieve? Commented Apr 18, 2013 at 22:59
  • Thanks for your comments guys. I've edited my question to explain the code. Commented Apr 19, 2013 at 5:41

1 Answer 1

5

Component links are typically anchor tags with a tcm id or have a "tridion" prefix on the href attribute, e.g. <a tridion:href="tcm:a:b">blah</a>. You can extract this from the entire component xml by running it through a regular expression. Here is a sample (I'm sure the regex can be optimized, alas, it works):

Regex TridionHrefExpression = new Regex(@"(?<tagname><(?<a>[^\s>]+)[^>]+tridion:href[^>]+?>)|(?<option><option.+tridion:value[^>]+>)", RegexOptions.Multiline | RegexOptions.Compiled);

foreach (Match matchingElement in TemplateUtilities.GetRegexMatches(outputContainer, TridionHrefExpression))
{
    linkTag = matchingElement.Groups[0].Value;

    //.. do stuff
}
2
  • 5
    You might also consider just using an XPath expression to iterate through those nodes as the content is stored as XML //@xlink:href[contains(., 'tcm:')] should give you all the href attributes containing TCMURIs inb the Component XML Commented Apr 19, 2013 at 0:06
  • Chris is right. Why on earth would you use a regex to find an XML Attribute?? In fact, I think you can also say that the xlink:href attributes will all be tcm uris. Commented Apr 20, 2013 at 18:10

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.