Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session.GetObject in Tridion 2011 returns component for non-existing item

I'm trying to figure out how to check if a component is null (e.g. not found in Tridion) in a custom backend. I just don't see how to check this, other than catching the error.

Component comp = (Component)session.GetObject(base.ComponentUri);

if (component != null)
{
    bool isCheckedOut = component.IsCheckedOut;
}
else
{
    // how do I get in here!
}

I'm 100% sure the uri is correct ('tcm:113-438134') and 100% sure that there's no item in Tridion with that URI. The code above will throw an error at component.IsCheckedOut:

The item tcm:113-438134-16 does not exist.

How do I check the existence of a Tridion item? I've tried placing the cast at the end, which didn't help much:

Component comp = session.GetObject(base.ComponentUri) as Component;

and I've tried not casting at all but using an IdentifiableObject, which also didn't make any difference. Anyone knows how to do this?

like image 707
Reinder Wit Avatar asked Jul 10 '12 11:07

Reinder Wit


1 Answers

This is because of Tridion's lazy loading - It will never return a null object if the URI seems valid. You can use session.IsExistingObject(TcmUriOrWebdavUrl) to check if the object exists.

And...

I'm sure you're expecting this...

in a custom backend

You cannot use the TOM.NET API for processes other than Templates and Event System. Please use the Core Service API instead.

like image 71
Nuno Linhares Avatar answered Nov 04 '22 15:11

Nuno Linhares