Last active
October 28, 2015 08:58
-
-
Save aflyen/f779232ec529d45c524f to your computer and use it in GitHub Desktop.
This file contains 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
function Enable-CustomItemScheduling { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true)] | |
[Microsoft.SharePoint.Client.Web]$Web, | |
[Parameter(Mandatory=$true)] | |
[String]$PagesLibraryName | |
) | |
$List = $Web.Lists.GetByTitle($PagesLibraryName) | |
# Get function from: https://gist.github.com/aflyen/4a098b69b9faa43fd1a3 | |
$ListParameter = Get-CustomLoadParameter -Object $List -PropertyName "EventReceivers" | |
$Web.Context.Load($List, $ListParameter) | |
Execute-SPOQuery | |
# Prerequisites for using scheduling | |
$List.EnableModeration = $true | |
$List.EnableMinorVersions = $true | |
$List.Update() | |
$Assembly = "Microsoft.SharePoint.Publishing, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" | |
$FullName = "Microsoft.SharePoint.Publishing.Internal.ScheduledItemEventReceiver" | |
$EventReceiverItemAddedExists = $false | |
$EventReceiverItemAddedType = [Microsoft.SharePoint.Client.EventReceiverType]::ItemAdded | |
$EventReceiverItemAddedName = "Item Added Event Handler For Scheduling" | |
$EventReceiverItemUpdatingExists = $false | |
$EventReceiverItemUpdatingType = [Microsoft.SharePoint.Client.EventReceiverType]::ItemUpdating | |
$EventRecieverItemUpdatingName = "Item Updating Event Handler For Scheduling" | |
# Check if the event receivers already exists | |
foreach ($EventReceiver in $List.EventReceivers) | |
{ | |
if ($EventReceiver.ReceiverName -eq $EventReceiverItemAddedName) | |
{ | |
$EventReceiverItemAddedExists = $true | |
} | |
elseif ($EventReceiver.ReceiverName -eq $EventRecieverItemUpdatingName) | |
{ | |
$EventReceiverItemUpdatingExists = $true | |
} | |
} | |
# Add event receiver for ItemAdded | |
if ($EventReceiverItemAddedExists -ne $true) | |
{ | |
$EventReceiverItemAdded = New-Object Microsoft.SharePoint.Client.EventReceiverDefinitionCreationInformation | |
$EventReceiverItemAdded.EventType = $EventReceiverItemAddedType | |
$EventReceiverItemAdded.ReceiverName = $EventReceiverItemAddedName | |
$EventReceiverItemAdded.ReceiverClass = $FullName | |
$EventReceiverItemAdded.ReceiverAssembly = $Assembly | |
$List.EventReceivers.Add($EventReceiverItemAdded) | Out-Null | |
} | |
# Add event receiver for ItemUpdating | |
if ($EventReceiverItemUpdatingExists -ne $true) | |
{ | |
$EventReceiverItemUpdating = New-Object Microsoft.SharePoint.Client.EventReceiverDefinitionCreationInformation | |
$EventReceiverItemUpdating.EventType = $EventReceiverItemUpdatingType | |
$EventReceiverItemUpdating.ReceiverName = $EventRecieverItemUpdatingName | |
$EventReceiverItemUpdating.ReceiverClass = $FullName | |
$EventReceiverItemUpdating.ReceiverAssembly = $Assembly | |
$List.EventReceivers.Add($EventReceiverItemUpdating)| Out-Null | |
} | |
# Make fields for start and end date visible and add them to the default view | |
if ($EventReceiverItemAddedExists -ne $true -or $EventReceiverItemUpdatingExists -ne $true) | |
{ | |
$FieldPublishingStartDateName = "PublishingStartDate" | |
$FieldPublishingExpirationDateName = "PublishingExpirationDate" | |
$FieldPublishingStartDate = $List.Fields.GetByInternalNameOrTitle($FieldPublishingStartDateName) | |
$FieldPublishingStartDate.Hidden = $false | |
$FieldPublishingStartDate.Update() | |
$FieldPublishingExpirationDate = $List.Fields.GetByInternalNameOrTitle($FieldPublishingExpirationDateName) | |
$FieldPublishingExpirationDate.Hidden = $false | |
$FieldPublishingExpirationDate.Update() | |
$ListDefaultView = $List.DefaultView | |
$ListDefaultView.ViewFields.Add($FieldPublishingStartDateName) | |
$ListDefaultView.ViewFields.Add($FieldPublishingExpirationDateName) | |
$ListDefaultView.Update() | |
$List.Update() | |
} | |
Execute-SPOQuery | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires an additional function "Get-CustomLoadParameter" found here: https://gist.github.com/aflyen/4a098b69b9faa43fd1a3