Following my recent blog about extension methods. Here are a few useful extension methods I’ve created recently for working with Tridion’s TOM API.
.NET Interops – COM API (5.2 and onwards)
Here are a few useful methods to use with the older TOM API (interops), these can come in handy when writing custom tools, event system code, etc.
Publish
The publish method for both Page and Component objects require quite a few parameters which in many cases are always set the same within a single application scope. If you want to use defaults for these values you can use these extension methods to predetermine the values of the parameters leaving a shorter and more convenient way for sending the items to be published:
For Components:
1 2 3 4 5 6 7 8 |
/// <summary> /// Offers a simple call to publish a components using defaults /// for all of the publish parameters  /// </summary> /// <param name="targets">one or more pucliation targets or target /// types to publish to /// </param> public static void Publish(this Component component, object targets, EnumPublishPriority priority) { component.Publish(targets, false, false, false, DateTime.Now, DateTime.MinValue, DateTime.Now, true, priority, true, 100); } |
For Pages:
1 2 3 4 5 6 7 8 |
/// <summary>
/// Offers a simple call to publish a components using defaults
/// for all of the publish parameters
/// </summary>
/// <param name="targets">one or more pucliation targets or
/// target types to publish to
/// </param>
public static void Publish(this Page page, object targets,
EnumPublishPriority priority)
{
page.Publish(targets, false, false, false, DateTime.Now,
DateTime.MinValue, DateTime.Now, true, priority, true, 100);
}
|
UnPublish
Very similarly to the publish method, here’s an example for creating a shorter way to call un-publish on a page object:
1 2 3 4 |
public static void UnPublish(this Page page, object targets,
EnumPublishPriority priority)
{
page.UnPublish(targets, true, false, false, DateTime.Now,
false, priority);
}
|
IsPublished
Tridion provides a method to check whether a page is published to a specific Target Type or Publication Target but what if you just want to check whether the page is published or not?
You need to check each target.
Here’s a way to add a method to the page type that will check whether a page is published by providing a list of targets to the method call:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/// <summary>
/// Assert whether a page is published or not
/// </summary>
/// <param name="page">The page in question</param>
/// <param name="targets">A list of target types or
/// publication targets to check
/// </param>
/// <returns>True if the page is published to at least
/// one of the provided targets</returns>
public static bool IsPublished(this Page page, IEnumerable<string> targets)
{
foreach (var t in targets)
{
if (page.IsPublishedTo(t)) return true;
}
return false;
}
|
Release
Its very important to make sure you correctly dispose of the Tridion COM objects exposed by the interops.
Here’s an example of how to easily do that for components and pages without having to add: “using System.Runtime.InteropServices;” to every code file:
For Component:
1 2 3 4 5 6 7 |
/// <summary>
/// Releases the COM Component object
/// </summary>
public static void Release(this Component component)
{
Marshal.ReleaseComObject(component);
}
|
For Page:
1 2 3 4 5 6 7 |
/// <summary>
/// Releases the COM Page object
/// </summary>
public static void Release(this Page page)
{
Marshal.ReleaseComObject(page);
}
|
TOM.NET (5.3 and onwards)
Fields
One of the things I was quite astonished to find when i first started working with the TOM.NET API was that the Component object no longer exposed a fields collection as a property. Instead to get an instance of those fields you are required to write code which is not very intuitive and definitely shouldnt be required for such a basic property.
I’ve since filed a formal request to our R&D department to change this but there’s no guarantee that this is going to change anytime soon.
If you are using Tridion 2009 and .NET 3.5 or brave enough to write your .NET templates on 5.3 or 5.3 SP1 with .NET 3.5 you might be interested in using the following extension method:
1 2 3 4 5 6 7 |
/// <summary>
/// Returns an ItemFields collection for the Component
/// </summary>
public static ItemFields Fields(this Component component)
{
return new ItemFields(component.Content, component.Schema);
}
|
With this method in place to get the fields collection of a component you only need the following: comp.Fields()…
Root StructureGroup
Another oddity in the relatively new API is that the publication object doesnt expose a RootStructureGroup property while it does expose a RootFolder property. Slightly annoying but easily fixed with… you guessed it! An extension method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/// <summary>
/// Returns the root structure group for this publication
/// </summary>
public static StructureGroup RootSG(this Repository publication)
{
Filter filter = new Filter();
filter.Conditions["ItemType"] = ItemType.StructureGroup;
IList<RepositoryLocalObject> items = publication.GetItems(filter);
if (items.Count == 0)
return null;
else
return (StructureGroup)items[0];
}
|
Conclusion
These are just a few useful methods that can make your Tridion development quicker and more convenient.
Undoubtedly there’s many more such methods people can think of, if you do have some that you use please share them and if we have enough I will create an extension on World with all of them!
Code
You can download the methods ive shown in this article here: code.
for all of the
Hey Yoav,
Pretty neat article 🙂
One thing I wanted to ask was that I am working on Tridion R5.3 so I cant create Components programatically using the .NET API.
Does Tridion 2009 support both Read/Write via the .NET API ?
Thanks Kunal!
Unfortunately, Tridion 2009 does not yet support the full read/write .NET. It is still read-only.
Tridion 2010 however will supply us with the full read/write API. According to the roadmap its “envisioned” to be released in Q3 of next year.
Cast your Repository to Publication and you’ll find your familiar RootStructureGroup.
((Publication)repository).RootStructureGroup;
Thanks Puf,
Any reason why its not in the Repository object as the RootFolder property is?
BTW, The code i’ve shown for this method is identical to the one of the Tridion property so the question is then whats more expensive, casting or the extension-method lookup? 😉
Just to set things clear, based on a little bit of research, extension methods do not cost anything since they are resolved at compile time.
There is another way of finding IsPublished status. I think your method is more generic. Thanks.
http://stringwriter.com/2011/01/13/finding-ispublished-in-tridion/
Great link, thanks.
Notice though that my method and the one on StringWriter are different.
The former is for pages and the latter for components. Also my example checks whether the page is published for specific target types.