Only get published pages in SharePoint

Sometimes in SharePoint you have to provide a summary page that shows a list article pages (like a news listing or events listing).  You can get all the pages in the pages library, but that includes pages that are not published.

In order to get around this problem you need to check if the item has a published version and check that the version level is Published.

Here is a sample that loops through a collection of pages only getting the published pages and adds them to a separate collection that will be used.

foreach (SPListItem listItem in listItemCollection)             
{
     if (listItem.HasPublishedVersion)                  
     {
          SPListItemVersionCollection itemVersionCollection = listItem.Versions;

foreach (SPListItemVersion itemVersion in itemVersionCollection)
{
if (itemVersion.Level == SPFileLevel.Published)
{
pressReleaseItems.Add(new PressReleaseList(Mask.ConvertToString(itemVersion[“PressReleaseTitle”]),
(DateTime.Parse(Mask.ConvertToString(itemVersion[“PressReleasePostedDate”]))).ToString(“MM/dd/yy”),                                                            Mask.ConvertToString(itemVersion[“PageSummary”]),
Mask.ConvertToString(itemVersion[“FileLeafRef”])));

break;
}
}
}
}

Advertisements

Got something to add?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s