Telerik.Sitefinity.GenericContent.Model.ContentItem with id

Posted by Community Admin on 04-Aug-2018 09:10

Telerik.Sitefinity.GenericContent.Model.ContentItem with id (...) is not a draft.

All Replies

Posted by Community Admin on 07-Jul-2011 00:00

I was going to report a bug, but found a solution to my problem while explaining the issue. Since I bothered typing everything below, I guess I'll just post it anyway. :)
Feel free to move it to another forum.

I got into a weird issue. I create Content blocks, and then try to retrieve their content from C# code.
For example:

ContentItem introBlockItem = App.WorkWith().ContentItems().FirstThat(c => c.Title == "FilterSearchIntro").Get();

The problem I get into is that this works with some Content blocks, but not with others.

I create a "FilterSearchIntro" block. It works with it.
I create a "FilterSearchHome" block. It doesn't work (I get an InvalidOperationException: "Telerik.Sitefinity.GenericContent.Model.ContentItem with id (...) is not a draft.").
I create a "FilterSearchIntro2" block. It doesn't work.
I create a "TEST" block. It works.
I create an "abc" block. It works.
I delete "FilterSearchHome" and recreate it. It still doesn't work.
I create a "FilterSearchHome3" block. It doesn't work.

At that point, I wonder if Sitefinity just doesn't like the names I choose, or something.
Retrieving blocks using Guids leads to the same exception.

Then, I tried to retrieve all content blocks (doing var x = App.WorkWith().ContentItems().Get()). The collection contains 4 elements while I have only 2 Content blocks. Huh huh. 2 of them seem to be the same as the 2 others, with one notable gotcha: their Status property is different. It's either "Live" or "Master". To make my code work consistently, I had to do the following:

ContentItem introBlockItem = App.WorkWith().ContentItems().FirstThat(c => c.Title == "FilterSearchIntro" && c.Status == ContentLifecycleStatus.Master).Get();

In other words, the problem was that "FirstThat" sometimes retrieved the wrong object (the "Live" one), since there were always 2 objects that matched a given Title or Guid.

Another way that works:

ContentItem introBlockItem = App.WorkWith().ContentItems().Drafts().FirstThat(c => c.Title == "FilterSearchIntro").Get();

This thread is closed