Super Charge Your JavaScript Development With TypeScript

Though my current writings on this blog may not show it, I’m more than just a Tridion and .NET nerd. Don’t get me wrong, I’m a huge .NET fan. But I love other technologies as well… one of them being JavaScript. Sure JavaScript may have gotten a bad rep as being a slow, crappy client side scripting language. And due to its heavy use in browsers, a lot of JavaScript development is done by non-programmers who don’t know any better, so a lot of what you see out there could have been done poorly in pretty much any language if done by the same group. But underneath the hood when you dive into its capabilities, there lies something beautiful (and not so slow when tuned correctly!). And I truly believe that there also lies the future of web technologies.

I’ve been working heavily with NodeJS in a couple of my side projects. For those who have not heard of this, NodeJS is a server side JavaScript technology. Now this is where I may get some eye rolls from fellow colleagues… JavaScript on the server? Now that’s gotta be buggy and slow, right? And at one point in my life (probably just a little over a year ago), I would have full heatedly agreed that the server was no place for some error prone JavaScript. But after much investigation into Node (and thus into JavaScript as well), I was in for such a surprise. A pleasant one.

Even with the new found love and understanding for this misunderstood technology, there was still something missing in my JavaScript project. Now I’m not talking about a project of a few hundred lines of code or less. I’m talking about one that has expanded into thousands. And the maintainability, especially for someone who has grown use to his conveniences of the tools in Visual Studio, was slowly becoming a nightmare.

And then recently, a colleague of mine introduced me to TypeScript, a “typed superset of JavaScript that compiles to plain JavaScript”. Yep, we’re talking about strongly typed JavaScript, classes, interfaces, and even some ES6 future features. The best part is, unlike some of those other languages that compile into JavaScript, TypeScript IS JavaScript. You can copy paste existing JavaScript code into your TypeScript file, and it’ll work. The best part is, with Visual Studio 2012 (and the plugin), you now have full intellisense support with your TypeScript JS, refactoring, and even code lookup (yep, that’s right, F12 it).

So, those maintainability issues that I was having? Yep, you guessed it… I’m currently in the process of re-writing my code into TypeScript style classes (not so much trouble since I was already using classes in JavaScript, function style). I’ll probably write up some posts on things that I’ve learned, probably maybe even an example of a GUI Extension with it as well. In the meantime, if you’re someone who’s writing some pretty complex JavaScript applications (client or server), I’d definitely recommend giving it a try. It’s not perfect yet, bit it has rekindled the fun in my JavaScript projects.

Getting Using and Used Items With TOM.NET

Periodically I will receive an e-mail from someone in the Tridion community asking questions like “How do I get the pages where a component is used?” or “How do I get all the components using a particular schema?”. Although there is already some good examples out there on retrieving Where Using and Where Used, I figured some more Tridion samples out there could never hurt.

GetUsingItems

The Tridion.ContentManager.IdentifiableObject (the base class that most of the other items in Tridion inherits) has a method called “GetUsingItems” that allows you to get all items using this item. Note that these are the items you see in the “Used in” tab when you use the “Where Used” functionality in the GUI. The UsingItemsFilter argument allows you to refine your query of using items even further.


IdentifiableObject.GetUsingItems()
IdentifiableObject.GetUsingItems(UsingItemsFilter filter)

Getting all pages used by a component:

UsingItemsFilter filter = new UsingItemsFilter(component.Session);
filter.ItemTypes = new List<ItemType> { ItemType.Page };
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;

IEnumerable<IdentifiableObject> pages = component.GetUsingItems(filter);

Getting all components used by a schema:

UsingItemsFilter filter = new UsingItemsFilter(schema.Session);
filter.ItemTypes = new List<ItemType> { ItemType.Component };
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;
filter.InRepository = (Repository)publication; // assuming variable publication is of type Tridion.ContentManager.CommunicationManagement.Publication and the only publication you want the results to return components for

IEnumerable<IdentifiableObject> components = schema.GetUsingItems(filter);

Getting all schemas that used a given embedded schema:

UsingItemsFilter filter = new UsingItemsFilter(embeddedSchema.Session);
filter.ItemTypes = new ItemType[] { ItemType.Schema }; // Note can be an array too
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;

IEnumerable<Schema> schemas = (IEnumerable<Schema>)embeddedSchema.GetUsingItems(filter);

Getting all components using a gien keyword:

UsingItemsFilter filter = new UsingItemsFilter(keyword.Session);

filter.ItemTypes = new List<ItemType> { ItemType.Component };
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;

IEnumerable<IdentifiableObject> components = keyword.GetUsingItems(filter);

Note that the above could have just been retrieved using the keyword’s GetClassifiedItems() method:

IEnumerable<RepositoryLocalObject> components = keyword.GetClassifiedItems();

GetUsedItems

The “GetUsedItems” method of IdentifiableObject works very similarly to the “GetUsingItems” method, but in reverse: it’ll grab items that this item uses. This is the same functionality as the “Uses” tab in the “Where Used” functionality of the GUI.


IdentifiableObject.GetUsedItems();
IdentifiableObject.GetUsedItems(UsedItemsFilter filter);

Get the embedded schemas used in a given schema:

UsedItemsFilter filter = new UsedItemsFilter(schema.Session);
filter.ItemTypes = new ItemType[] { ItemType.Schema };

IEnumerable<IdentifiableObject> embeddedSchemas = schema.GetUsedItems(filter);

Get Components Within ComponentLinks of a given component:

UsedItemsFilter filter = new UsedItemsFilter(component.Session);
filter.ItemTypes = new ItemType[] { ItemType.Component };

var compLinks = component.GetUsedItems(filter);

Getting the List as XML

When working with a pretty large set of items that may get returned, you may want to work with an XML of the used items instead for performance reasons. The following methods return a type of XmlElement:


IdentifiableObject.GetListUsedItems();
IdentifiableObject.GetListUsedItems(UsedItemsFilter filter);
IdentifiableObject.GetListUsingItems();
IdentifiableObject.GetListUsingItems(UsingItemsFilter filter);