Items are null in Sitecore 6
Asked Answered
W

3

5

Sometimes Sitecore will not return certain items. For instance, in the following code:

Sitecore.Data.ID itemId = new Sitecore.Data.ID(id);
Sitecore.Context.Database.GetItem(id);

This will return null, however if the same string id is placed on the search above the content tree in the content editor, it will return the item! The context database is web and I've republished the item several times.

I am using Sitecore 6.2.0 (rev. 100701).

Waly answered 7/12, 2010 at 16:32 Comment(1)
I rolled the question back, as it had been updated to reflect the answer - so no longer made sense.Twopenny
R
5

Are you sure it is not a typo? I mean, shouldn't you pass itemId instead of just id to GetItem?

I don't remember exactly how it works, but if you pass a string to this method, it probably treats it as path, not ID, and tries to resolve the item by path. You can check this much faster than I do:

Sitecore.Data.ID itemId = new Sitecore.Data.ID(id);
Sitecore.Context.Database.GetItem(itemId);
Reward answered 7/12, 2010 at 17:28 Comment(4)
yeah im sure, sorry my mistake because of rushed copy pasting, im using some utility function: public static Sitecore.Data.Items.Item GetItemById (Sitecore.Data.ID id) { return Sitecore.Context.Database.GetItem(id); }Waly
Probably, the item is still not in the web database? It might have publish restrictions or not have passed workflow... Try simple "select * from Items where ID = 'YOUR-GUID-HERE'" against web DB to make sureReward
ok i found out what the cause is... it seems that sitecore does not like when the context sitecore user is extranet/anonymous. I log in as a regular user and it works...Waly
That's because extranet\anonymous doesn't have read access to that very item. Great that it is solved nowReward
S
5

I just had the same issue - GetItem() function returns NULL

After pulling my hair for about an hour, I found that my GetItem() function is called by the front end and the extranet/Anonymous user doesn't have read access to the item that I'm trying to get.

So, remember to check the security !

Sandstorm answered 28/9, 2011 at 20:24 Comment(0)
L
0

refer the following article. http://sdn.sitecore.net/Articles/API/Context%20and%20databases.aspx

on web.config the site name="website" node may be pointing to the wrong database. The database property should be "master" or "web".

Legalism answered 13/5, 2014 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.