Archive
LINQ to Tridion
Contents
Introduction
Its Friday evening, a good time to share a little something I’ve been working on.
.NET 3.0 brought with it a fantastic new language (VB, C#) extension called LINQ which stands for Language-Integrated Query.
Microsoft describes it as “a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.”
In short it gives the developer a more general syntax for querying sets or collections of data.
The beautiful thing about LINQ is that it can be used over any collection whether its XML, Database or even objects so long as they support the IEnumerable<T> interface.
For more information see the resources section of this article or simply google/bing it.
A Few Useful Tridion Extension Methods
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
