I'm trying to retrieve all blog entries where a specified user is the author. I want results from all blogs and I want them sorted in CreatedDate order. So far, so good, no problem. But I also want to filter away unpublished entries and I can't figure out how to do that. The condition is that the db column datPublicationEnd is null. How do I configure my EntryQuery to achieve that? My code is below:
UserAuthorCriterion criterion = new UserAuthorCriterion();criterion.User = new UserCriterion();criterion.User.ID = new IntegerCriterion();criterion.User.ID.Value = user.ID;EntryQuery eq = new EntryQuery();eq.Author = criterion;eq.Created = new DateTimeCriterion();eq.OrderBy.Add(new CriterionSortOrder(eq.Created, SortingDirection.Descending));
// missing code to filter out unpublished entries
return StarCommunity.Modules.Blog.BlogHandler.GetQueryResult(eq, page, numberOfItems, out total);
Notice that I am looking for entries where the user is the author, not the reader, so I cannot use one of the regular calls to BlogHandler.GetEntries() (right?)
First of all I would advice you not to look at datPublicationEnd, since there can be entries that are published even if datPublicationEnd is not null. This is the case for entries that have datPublicationEnd > DateTime.Now.
Since there is no straight forward way to get what you are after, I would sugest you to look at datPublicationStart with the condition that this should be less than (or equal to) DateTime.Now. In this case you could get entries that have datPublicationEnd < DateTime.Now, but maybe you don't have the possibility to submit a end date for your entries on the specific site?! If this is the case, you could use this work around.
Hope this helps!
In this particular solution, you cannot choose an end date for the publication, once it's out, it's out, so to say. If there is a value in the datPublicationEnd column, that value will, as you say, be less than the startdate, to indicate that the entry is not published. When the user chooses to publish the entry, the value will be set to null. However, I am in a postion where I can change this behavior if I want. Let's say that we set the end publication date to the year 3000 or whatever instead of null, to indicate that the entry is published. Would that help? Would there be a way to get what I want then? Could I supply the EntryQuery object with the two conditions that the startdate needs to be < DateTime.Now and the end date > DateTime.Now?
The solution you sugest would solve your problem and should work fine. But I would solve it like this:
When you create the entry, use this constructor:
Entry entry = new Entry(blog, author, titleText, contentText);
Set the publication start date, if you have it:
entry.PublicationStart = DateTime.Now;
Or set a date in the future if the author wants to delay the publication, for example one month later:
entry.PublicationStart = DateTime.Now.AddMonth(1);
Now you only have to look at publication start date. The value of datPublicationEnd will always be null in the database, but in your scenario this doesn't matter.Because as long as you have the date set in the datPublicationStart that is less then DateTime.Now the entry will be published.
So to wrap up, set the criteria:
entry.PublicationStart < DateTime.Now