Reputation: 32694
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?
Upvotes: 1
Views: 5027
Reputation: 13266
The following is an example of the CAML query for checking a document is published. I'm aware this is a fairly old question but hopefully this might be of use to the next person who googles how to do this:
<Query>
<Where>
<And>
<Or>
<Leq>
<FieldRef Name='PublishingStartDate'/>
<Value Type='DateTime' IncludeTimeValue='TRUE'>
<Today/>
</Value>
</Leq>
<IsNull>
<FieldRef Name='PublishingStartDate'/>
</IsNull>
</Or>
<Or>
<Geq>
<FieldRef Name='PublishingExpirationDate'/>
<Value Type='DateTime' IncludeTimeValue='TRUE'>
<Today/>
</Value>
</Geq>
<IsNull>
<FieldRef Name='PublishingExpirationDate'/>
</IsNull>
</Or>
</And>
</Where>
</Query>
Upvotes: 2
Reputation: 3777
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
Upvotes: 2