Last active
January 13, 2018 18:34
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<## | |
# 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