Documentation Center

Handling fields that contain Component Links

Some Components contain a Component Link field, that is, a reference to a Component.

Package example

To handle that Component in a package, simply access the value of the Component Link field. This contains the URI (unique identifier) of the target Component, which you can then process further.

// The Component has a field of type Component Link named SeeAlso.
// The value of this field is the Content Manager URI of the Component it links to.
string uri = package.GetValue("Component.Fields.SeeAlso");
// Use the GetObject() method to retrieve this target Component.
Component linkedComponent = (Component) engine.GetObject(uri);
// Now retrieve the value of the Headline field of the Component it links to.
ItemFields itemFields = new ItemFields(linkedComponent.Content, linkedComponent.Schema);
string headline = itemFields["Headline"].ToString();

Content Manager example

To access the Component Link target in the Content Manager, navigate the data structure as in the following example:

// Define a filter to return only Components.
Filter filter = new Filter();
filter.Conditions["ItemType"] = ItemType.Component;
// Get the Publication identified by publicationURI;
Publication publication = (Publication) engine.GetSession().GetObject(publicationURI);
// Get the list of Components in the root Folder of the Publication;
IList<RepositoryLocalObject> list = publication.RootFolder.GetItems(filter);
Component component = (Component)list[1];
// Get the Fields of the first Component in that list;
ItemFields fields = new ItemFields(component.Content, component.Schema);
// Check for a field called SeeAlso, and verify that it is embedded.
if (fields.Contains("SeeAlso"))
{
	ComponentLinkField linkField = fields["SeeAlso"] as ComponentLinkField;
	Component linkedComponent = linkField.Value;
}