GetObject vs Get(ItemType)

This is more of a ‘note to self’ as I’m constantly being caught out when getting an object from Tridion using the various TDSE methods.

I’ll use the example of getting a folder via the TDSE object.  To do this you have two ways:

  • The TDSE.GetFolder(“ID”, Publication Object) Method
  • The TDSE.GetObject(“ID”, OpenMode, Publication, ReadFilter) Method

The GetFolder will always need a valid Publication object, where as the GetObject will allow a null to be thrown in.

I’ve done this a thousand times:

  • TDSE.GetFolder(“tcm:1-123-2”, null);

Then spent about 20 minutes checking the XML, the webdav etc etc.

The benefit of being able to pass the null, is that if the Publication object doesn’t exist, you don’t have to create it surely making the GetObject a far superior method to use… just remember to cast to the correct Tridion item type:

  • Folder fld = tdse.GetObject(…) as Folder

Please tell me I’m not alone in being constantly caught out by this!

5 thoughts on “GetObject vs Get(ItemType)

  1. There’s an easy solution! Just use TDSE.GetObject() for everything. The “typed” variants, such as GetFolder are documented as “for backwards compatibility”. The preferred way is to use GetObject()

  2. According to the doc, these methods (GetFolder, GetComponent, etc) have been deprecated and you should always use GetObject instead…

    “This method is exposed for backward compatibility only. It is advised to use TDSE.GetObject(…) or TDSE.GetNewObject(…) since they offer more flexibility than this method. “

  3. Also… we tend to use:
    Folder folder = tdse.GetObject (…) as Folder;

    This will not throw an error if the folder happens to be null, so you can quickly check if(folder != null)…

  4. the other thing I’ve started doing when casting, is using ‘as’ instead of the brackets () ie:

    Folder fld = GetObject(…) as Folder;

    this will prevent an exception being thrown when the object isn’t a folder, and allow you to test
    if (fld != null) {
    //do something with folder;
    but maybe in some circumstances allowing and catching the exception is better.

  5. @Nuno and @Marcemarc – thanks for the ‘as Folder’ tip will update my notes.

    @Dominic – Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>