String Writer

my tech stuffs…

Posts Tagged ‘Tips

Getting keywords from category in Tridion

leave a comment »

As part of my requirements, I needed to navigate all the keywords from all Tridion categories. So I have written a utility class which will loop through all the keywords in a publication. Here I have given a part of it which will explain about keyword retrieval part.

In Tridion, a category will contain all the keywords so in order to retrieve all the keywords, we need to have category object first. Category object does provide a method, GetKeywordByTitle, to retrieve a keyword by passing its title as parameter. But the problem is when you pass a wrong name to this method, this will throw an exception instead of returning null.

If you are familiar with .Net/JAVA programming, while you are retrieving an item from a collection, you will always expect null to be returned if an item does not exists. Hope this will be solved in Tridion 2011 J

So I’ve added a utility method which would return null even if the keyword doesn’t exists in the category.

 

 

private Keyword GetKeywordByTitle(Category catObj , string keyword)
{
try
{
return catObj.GetKeywordByTitle(keyword);
}
catch
{}
return null;
}


The above method can be called when an keyword object is going to retrieved as shown below.

 

private void TestMethod()
{
TDSE tObj = new TDSE();
Category catObj = (Category)tObj.GetObject("tcm:50-51-512", EnumOpenMode.OpenModeEdit, PublicationTCMURI, XMLReadFilter.XMLReadAll);
Keyword kObj = this.GetKeywordByTitle(catObj, “Chennai”);
if (kObj == null)
   Console.WriteLine(“{0} does not exists!”, keyword);
else
   Console.WriteLine(“{0} exists!”, keyword);
}

 

This is not a solution but just a tip for Tridion beginners J

 

Written by visvabalaji

June 2, 2010 at 2:11 am

Posted in Tridion

Tagged with , ,

LINQ : OfType<T>()

leave a comment »

OfType<T> : Another useful and commonly used extension methods in LINQ. Assume that you have an array or collection of values which is of multiple types and you are required to filter only the values of specific type.


public void Process(List<Account> accounts)
{
  Console.WriteLine("Available Credit Cards :");

  foreach (var cc in accounts.OfType<CreditCardAccount>())
  {
    Console.WriteLine(cc.Name);
  }

  Console.WriteLine("Available Savings Accounts :");

  foreach (var sb in accounts.OfType<SavingsAccount>())
  {
    Console.WriteLine(sb.Name);
  }
}

Here’s the output of this method.

clip_image001

Written by visvabalaji

January 17, 2007 at 5:58 am

Posted in C#, LINQ

Tagged with , ,

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: