The Tridion TOM.NET API and the Core Service introduced some new ItemTypes to Tridion, including the IdentifiableObject type. While this is not a UFO, it might appear to you as an Unidentifable (Flying) Object. In this post I hope to explain these other ItemTypes: IdentifiableObject, SystemWideObject, RepositoryLocalObject, and VersionedItem and are what I call the Tridion UFOs.
The Tridion Core Service methods return the IdentifiableObject method a lot and knowing more about this type and also how to cast it to a more specific type is very important to get a working solution. Recently I struggled a bit with this and was fortunate to have the Tridion Community on StackOverflow help me with my question.
In the old TOM API we did not have any generic parents to the objects – we always got back a concrete instance of an object that matched 1-1 to something we could see and touch in the GUI. This is not the case with TOM.NET or the Core Service where we can get back base-objects that contain a subset of the methods and properties of our real objects. These new classes are super-handy in .NET where we can use Generics and create more re-usable methods. But, using them requires a little bit of knowledge as to which one is best for the given scenario.
The Tridion 2011 UFOs
IdentifiableObject
The opposite of a UFO, but the most prevalent ItemType is the IdentifiableObject (IO). The favorite return type of many methods in TOM.NET and the Core Service, the IdentifiableObject always leaves us wanting more. Which is why we usually cast it to a more concrete object as soon as possible – unless we really only need the Title or URI.
Methods and Properties available: * means the property is mandatory.
- Title*
- ID
- IsEditable
- AllowedActions
- ExtensionData
SystemWideObject
Items not within a Publication. Most of these can be found in the Administration section of the GUI. Examples are Users and PublicationTargets.
Methods and Properties available:
- Same as IdentifiableObject
RepositoryLocalObject
Repository is another name for Publication (why couldn’t they call it PublicationLocalObject?) so this means all items within our Publication. This ItemType is a good generic one to use when dealing with most content types.
Methods and Properties available include all from IdentifiableObject plus:
- BlueprintInfo
- IsPublishedInContext
- LocationInfo
- Metadata
VersionedItem
Content items such as Pages and Components. Does not include non-versioned items such as Folders or Structure Groups.
Methods and Properties available include all from RepositoryLocalObject plus:
Tridion Old School Objects
Page
The page object we all know and love. Contains a mandatory property ‘FileName’ that must be set when creating new items.
Methods and Properties available include all from VersionedItem plus:
- ComponentPresentations
- FileName*
- IsPageTemplateInherited
- PageTemplate
- WorkflowInfo
Component
The classic object containing the actual content. Contains a mandatory property of Schema.
Methods and Properties available include all from VersionedItem plus:
- ApprovalStatus
- BinaryContent
- ComponentType
- Content
- IsBasedOnMandatorySchema
- IsBasedOnTridionWebSchema
- Schema*
- WorkflowInfo
Casting the Generic IdentifiableObject to a Page object
Recently I was working on porting a classic ASP custom page to use the Core Service and was excited to learn about the copy method on this StackOverflow post. By default it returns an IdentifiableObject:
// newItem is ItemType IdentifiableObject
var newItem = client.Copy(source.Id, orgItemUri, true, new ReadOptions());
If I want to access the FileName property I need to cast it to a Page object:
// newItem is ItemType Page
var newItem = client.Copy(source.Id, orgItemUri, true, new ReadOptions()) as Page;
However, in my code I wanted to copy any kind of item and cast it to a Page if I need to. With some help from the Tridion StackOverflow community I learned how to get the ItemType and then for only Pages set the FileName property.
PageData pageData = newItem as PageData; // Cast IdentifiableObject to Page object
Notice the new UnknownByClient (UBC) ItemType – almost a UFO!
private string CreateNewItemCopy(string title, RepositoryLocalObjectData source, string filename)
{
string newItemUri = "";
try
{
ItemType tridionItemType = GetTridionItemType(source);
string orgItemUri = source.LocationInfo.OrganizationalItem.IdRef;
var newItem = client.Copy(source.Id, orgItemUri, true, new ReadOptions());
newItem.Title = title;
if (tridionItemType == ItemType.Page)
{
PageData pageData = newItem as PageData;
pageData.FileName = filename;
client.Update(pageData, new ReadOptions());
}
else
{
client.Update(newItem, new ReadOptions());
}
newItemUri = newItem.Id;
}
catch (Exception ex)
{
throw;
}
return newItemUri;
}
private ItemType GetTridionItemType(RepositoryLocalObjectData source)
{
string itemType = source.GetType().Name;
switch (itemType)
{
case "ComponentData":
return ItemType.Component;
case "PageData":
return ItemType.Page;
}
return ItemType.UnknownByClient;
}
Tridion Object Casting Is Good
Don’t worry if the method returns an IdentifiableObject and you need a concrete object such as Page or Component. Cast it to the object type you need and you’ll be cooking with gas!
Summary
It is a lot of fun learning about the Core Service. However, sometimes it is difficult getting familiar with the new ItemTypes and limited properties they provide. Becoming familiar with them and casting is essential. What they offer allows us to use the most generic object possible for our methods yet also the opportunity to cast the object down to reality when you need to access that oh-so-special property. I hope I’ve provided a good overview of these UFOs and wish you luck in your casting adventures.