Skip to content

Instantly share code, notes, and snippets.

@koohq
Last active January 13, 2018 18:34
Show Gist options
  • Save koohq/65085dc93438d04909cbc57dcdfad19b to your computer and use it in GitHub Desktop.
Save koohq/65085dc93438d04909cbc57dcdfad19b to your computer and use it in GitHub Desktop.
Provides a filter to retrieve the last-published list items on SharePoint Lists.
/**
* SP-SelectLastPublishedVersionItem.cs
*
* (c) 2018 koohq. Licensed under CC0.
* https://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
namespace Kooh.Q.Hook
{
using Microsoft.SharePoint;
public static class SPXtension
{
/// <summary>
/// Select the last-published items from target items.
/// </summary>
/// <param name="items">source items</param>
/// <returns>An IEnumerable of SPListItem that is last-published</returns>
public static IEnumerable<SPListItem> SelectLastPublishedVersionItem(this SPListItemCollection items)
{
return items.OfType<SPListItem>()
.Where(x => x.HasPublishedVersion)
.Select(x =>
x.Versions.OfType<SPListItemVersion>()
.First(x => x.Level == SPFileLevel.Published)
.ListItem
);
}
}
}
<##
# SP-SelectLastPublishedVersionItem.ps1
#
# (c) 2018 koohq. Licensed under CC0.
# https://creativecommons.org/publicdomain/zero/1.0/legalcode
#>
filter Select-LastPublishedVersionItem
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
$Items
)
$Items |
where HasPublishedVersion |
foreach { ($_.Versions | where Level -EQ "Published" | select -First 1).ListItem }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment