views:

45

answers:

1

I am currently retrieving all pages and filtering out ones that are not published in the code, checking whether DateTime.Now is smaller than this:

static readonly DateTime IMMEDIATE_PUBLISH = new DateTime(1900, 1, 1);

public static DateTime PublicationDate(this SPListItem item)
{
    // get start publish date
    PublishingPage page = item.Publishing();
    if (page != null)
    {
        bool isPublished = (page.ListItem.File != null)
            ? (page.ListItem.File.Level == SPFileLevel.Published)
            : true;
        bool isApproved = (page.ListItem.ModerationInformation != null)
            ? (page.ListItem.ModerationInformation.Status == SPModerationStatusType.Approved)
            : true;
        if (isPublished && isApproved && (DateTime.Now < page.EndDate))
        {
            return page.StartDate == IMMEDIATE_PUBLISH ? page.CreatedDate : page.StartDate;
        }
        return DateTime.MaxValue;
    }
    // not a scheduled item. treat as published
    return DateTime.MinValue;
}

What would be the equivalent CAML query, so that I SharePoint doesn't pull unnecessary items from the database?

+1  A: 

Hi skolima

In my opion you're checking way too much.

You should only check "PublishingStartDate" <= Today and "PublishingExpirationDate" > Today

For ordinary users you'll not find pages that isn't published/approved.
For users with rights to find these pages you probably don't want to exclude them just because the current version isn't published/approved. If you only want pages where at least one version is published then you can add a check for "_UIVersion" >= 512

Per Jakobsen
The problem is that I'm trying to cache the results, and the initial query is being run with full rights. I'm looking into changing how my cache works right now.
skolima
But if you run with the criteria you specified in your question, then you'll exclude pages where a new version is in progress even though an old version is published/approved
Per Jakobsen