diff --git a/documentation/Get-PnPLibraryFileVersionBatchDeleteJobProgress.md b/documentation/Get-PnPLibraryFileVersionBatchDeleteJobProgress.md new file mode 100644 index 000000000..ba1f96252 --- /dev/null +++ b/documentation/Get-PnPLibraryFileVersionBatchDeleteJobProgress.md @@ -0,0 +1,65 @@ +--- +Module Name: PnP.PowerShell +schema: 2.0.0 +applicable: SharePoint Online +online version: https://pnp.github.io/powershell/cmdlets/Get-PnPLibraryFileVersionBatchDeleteJobProgress.html +external help file: PnP.PowerShell.dll-Help.xml +title: Get-PnPLibraryFileVersionBatchDeleteJobProgress +--- + +# Get-PnPLibraryFileVersionBatchDeleteJobProgress + +## SYNOPSIS +Get the progress of deleting existing file versions on the document library. + +## SYNTAX + +```powershell +Get-PnPLibraryFileVersionBatchDeleteJobProgress -Identity [-Connection ] +``` + +## DESCRIPTION +This cmdlet allows retrieval of the progress of deleting existing file versions on the document library. + +## EXAMPLES + +### EXAMPLE 1 +```powershell +Get-PnPLibraryFileVersionBatchDeleteJobProgress -Identity "Documents" +``` + +Returns the progress of deleting existing file versions on the document library. + +## PARAMETERS + +### -Identity +The ID, name or Url (Lists/MyList) of the document library to perform the trimming on. + +```yaml +Type: ListPipeBind +Parameter Sets: (All) + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByValue) +Accept wildcard characters: False +``` + +### -Connection +Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. + +```yaml +Type: PnPConnection +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +## RELATED LINKS + +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/documentation/Get-PnPSiteFileVersionBatchDeleteJobProgress.md b/documentation/Get-PnPSiteFileVersionBatchDeleteJobProgress.md new file mode 100644 index 000000000..b15393b11 --- /dev/null +++ b/documentation/Get-PnPSiteFileVersionBatchDeleteJobProgress.md @@ -0,0 +1,51 @@ +--- +Module Name: PnP.PowerShell +schema: 2.0.0 +applicable: SharePoint Online +online version: https://pnp.github.io/powershell/cmdlets/Get-PnPSiteFileVersionBatchDeleteJobProgress.html +external help file: PnP.PowerShell.dll-Help.xml +title: Get-PnPSiteFileVersionBatchDeleteJobProgress +--- + +# Get-PnPSiteFileVersionBatchDeleteJobProgress + +## SYNOPSIS +Get the progress of deleting existing file versions on the site. + +## SYNTAX + +```powershell +Get-PnPSiteFileVersionBatchDeleteJobProgress [-Connection ] +``` + +## DESCRIPTION +This cmdlet allows retrieval of the progress of deleting existing file versions on the site. + +## EXAMPLES + +### EXAMPLE 1 +```powershell +Get-PnPSiteFileVersionBatchDeleteJobProgress +``` + +Returns the progress of deleting existing file versions on the site. + +## PARAMETERS + +### -Connection +Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. + +```yaml +Type: PnPConnection +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +## RELATED LINKS + +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/documentation/New-PnPLibraryFileVersionBatchDeleteJob.md b/documentation/New-PnPLibraryFileVersionBatchDeleteJob.md index 71bf27f0a..d9ca6b4a9 100644 --- a/documentation/New-PnPLibraryFileVersionBatchDeleteJob.md +++ b/documentation/New-PnPLibraryFileVersionBatchDeleteJob.md @@ -30,14 +30,28 @@ Starts a file version batch trim job for a document library. New-PnPLibraryFileVersionBatchDeleteJob -Identity "Documents" -DeleteBeforeDays 360 ``` -Starts a file version batch trim job that will delete all file verions that are over 360 days old in the document library. +Starts a file version batch trim job that will delete all file versions that are over 360 days old in the document library. ### EXAMPLE 2 ```powershell New-PnPLibraryFileVersionBatchDeleteJob -Identity "Documents" -DeleteBeforeDays 360 -Force ``` -Starts a file version batch trim job that will delete all file verions that are over 360 days old in the document library, without prompting the user for confirmation. +Starts a file version batch trim job that will delete all file versions that are over 360 days old in the document library, without prompting the user for confirmation. + +### EXAMPLE 3 +```powershell +New-PnPLibraryFileVersionBatchDeleteJob -Automatic +``` + +Starts a file version batch trim job that will delete file versions that expiread and set version expiration time for the ones not expired in the document library based on the backend algorithm. + +### EXAMPLE 4 +```powershell +New-PnPLibraryFileVersionBatchDeleteJob -MajorVersionLimit 30 -MajorWithMinorVersionsLimit 10 +``` + +Starts a file version batch trim job that will delete file versions in the document library based on the version count limits. ## PARAMETERS @@ -60,7 +74,7 @@ The minimum age of file versions to trim. In other words, all file versions that ```yaml Type: int -Parameter Sets: (All) +Parameter Sets: DeleteOlderThanDays Required: True Position: Named @@ -69,6 +83,48 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Automatic +Trim file version using automatic trim. + +```yaml +Type: SwitchParameter +Parameter Sets: AutomaticTrim + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -MajorVersionLimit +Trim file version using version count limits. Need to specify MajorWithMinorVersionsLimit as well. + +```yaml +Type: int +Parameter Sets: CountLimits + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -MajorWithMinorVersionsLimit +Trim file version using version count limits. Need to specify MajorVersionLimit as well. + +```yaml +Type: int +Parameter Sets: CountLimits + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Force When provided, no confirmation prompts will be shown to the user. diff --git a/documentation/New-PnPSiteFileVersionBatchDeleteJob.md b/documentation/New-PnPSiteFileVersionBatchDeleteJob.md index 826d1c606..83d360265 100644 --- a/documentation/New-PnPSiteFileVersionBatchDeleteJob.md +++ b/documentation/New-PnPSiteFileVersionBatchDeleteJob.md @@ -30,14 +30,28 @@ Starts a file version batch trim job targeting all document libraries in a site New-PnPSiteFileVersionBatchDeleteJob -DeleteBeforeDays 360 ``` -Starts a file version batch trim job that will delete all file verions that are over 360 days old in all document libraries in the site collection. +Starts a file version batch trim job that will delete all file versions that are over 360 days old in all document libraries in the site collection. ### EXAMPLE 2 ```powershell New-PnPSiteFileVersionBatchDeleteJob -DeleteBeforeDays 360 -Force ``` -Starts a file version batch trim job that will delete all file verions that are over 360 days old in all document libraries in the site collection, without prompting the user for confirmation. +Starts a file version batch trim job that will delete all file versions that are over 360 days old in all document libraries in the site collection, without prompting the user for confirmation. + +### EXAMPLE 3 +```powershell +New-PnPSiteFileVersionBatchDeleteJob -Automatic +``` + +Starts a file version batch trim job that will delete file versions that expiread and set version expiration time for the ones not expired in the site collection based on the backend algorithm. + +### EXAMPLE 4 +```powershell +New-PnPSiteFileVersionBatchDeleteJob -MajorVersionLimit 30 -MajorWithMinorVersionsLimit 10 +``` + +Starts a file version batch trim job that will delete file versions in the site collection based on the version count limits. ## PARAMETERS @@ -46,7 +60,7 @@ The minimum age of file versions to trim. In other words, all file versions that ```yaml Type: int -Parameter Sets: (All) +Parameter Sets: DeleteOlderThanDays Required: True Position: Named @@ -55,6 +69,48 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Automatic +Trim file version using automatic trim. + +```yaml +Type: SwitchParameter +Parameter Sets: AutomaticTrim + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -MajorVersionLimit +Trim file version using version count limits. Need to specify MajorWithMinorVersionsLimit as well. + +```yaml +Type: int +Parameter Sets: CountLimits + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -MajorWithMinorVersionsLimit +Trim file version using version count limits. Need to specify MajorVersionLimit as well. + +```yaml +Type: int +Parameter Sets: CountLimits + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Force When provided, no confirmation prompts will be shown to the user. diff --git a/src/Commands/Lists/GetLibraryFileVersionBatchDeleteJobProgress.cs b/src/Commands/Lists/GetLibraryFileVersionBatchDeleteJobProgress.cs new file mode 100644 index 000000000..2769c3dd6 --- /dev/null +++ b/src/Commands/Lists/GetLibraryFileVersionBatchDeleteJobProgress.cs @@ -0,0 +1,58 @@ +using Microsoft.SharePoint.Client; + +using PnP.Framework.Utilities; +using PnP.PowerShell.Commands.Base.PipeBinds; +using PnP.PowerShell.Commands.Model.SharePoint; +using System; +using System.Management.Automation; +using System.Text.Json; + +namespace PnP.PowerShell.Commands.Lists +{ + [Cmdlet(VerbsCommon.Get, "PnPLibraryFileVersionBatchDeleteJobProgress")] + [OutputType(typeof(FileVersionBatchDeleteJobProgress))] + public class GetLibraryFileVersionBatchDeleteJobProgress : PnPWebCmdlet + { + [Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)] + [ValidateNotNull] + public ListPipeBind Identity; + + protected override void ExecuteCmdlet() + { + var list = Identity.GetList(CurrentWeb); + if (list != null) + { + var ret = list.GetProgressForDeleteFileVersions(); + ClientContext.ExecuteQueryRetry(); + + var progress = JsonSerializer.Deserialize(ret.Value); + var connectionUrl = new Uri(Connection.Url); + var serverUrl = string.Concat("https://", connectionUrl.Host); + progress.Url = string.Concat(serverUrl, list.RootFolder.ServerRelativeUrl); + + if (!string.Equals(progress.BatchDeleteMode, FileVersionBatchDeleteMode.DeleteOlderThanDays.ToString(), StringComparison.OrdinalIgnoreCase)) + { + progress.DeleteOlderThan = string.Empty; + } + + if (!string.Equals(progress.BatchDeleteMode, FileVersionBatchDeleteMode.CountLimits.ToString(), StringComparison.OrdinalIgnoreCase)) + { + progress.MajorVersionLimit = string.Empty; + progress.MajorWithMinorVersionsLimit = string.Empty; + } + + if (string.Equals(progress.LastProcessTimeInUTC, DateTime.MinValue.ToString())) + { + progress.LastProcessTimeInUTC = string.Empty; + } + + if (string.Equals(progress.CompleteTimeInUTC, DateTime.MinValue.ToString())) + { + progress.CompleteTimeInUTC = string.Empty; + } + + WriteObject(progress); + } + } + } +} diff --git a/src/Commands/Lists/NewLibraryFileVersionBatchDeleteJob.cs b/src/Commands/Lists/NewLibraryFileVersionBatchDeleteJob.cs index 98ffeb941..0109a3dc5 100644 --- a/src/Commands/Lists/NewLibraryFileVersionBatchDeleteJob.cs +++ b/src/Commands/Lists/NewLibraryFileVersionBatchDeleteJob.cs @@ -12,24 +12,70 @@ namespace PnP.PowerShell.Commands.Lists [Cmdlet(VerbsCommon.New, "PnPLibraryFileVersionBatchDeleteJob")] public class NewLibraryFileVersionBatchDeleteJob : PnPWebCmdlet { + private const string ParameterSet_AUTOMATICTRIM = "AutomaticTrim"; + private const string ParameterSet_DELETEOLDERTHANDAYS = "DeleteOlderThanDays"; + private const string ParameterSet_COUNTLIMITS = "CountLimits"; + [Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)] [ValidateNotNull] public ListPipeBind Identity; - [Parameter(Mandatory = true)] + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_AUTOMATICTRIM)] + public SwitchParameter Automatic; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_DELETEOLDERTHANDAYS)] public int DeleteBeforeDays; + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_COUNTLIMITS)] + public int MajorVersionLimit; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_COUNTLIMITS)] + public int MajorWithMinorVersionsLimit; + [Parameter(Mandatory = false)] public SwitchParameter Force; protected override void ExecuteCmdlet() { + FileVersionBatchDeleteMode batchDeleteMode; + if (Automatic) + { + batchDeleteMode = FileVersionBatchDeleteMode.AutomaticTrim; + DeleteBeforeDays = -1; + MajorVersionLimit = -1; + MajorWithMinorVersionsLimit = -1; + } + else if (ParameterSpecified(nameof(DeleteBeforeDays))) + { + batchDeleteMode = FileVersionBatchDeleteMode.DeleteOlderThanDays; + MajorVersionLimit = -1; + MajorWithMinorVersionsLimit = -1; + } + else if (ParameterSpecified(nameof(MajorVersionLimit)) || + ParameterSpecified(nameof(MajorWithMinorVersionsLimit))) + { + batchDeleteMode = FileVersionBatchDeleteMode.CountLimits; + DeleteBeforeDays = -1; + } + else + { + throw new PSArgumentException($"One or more parameters issued cannot be used together or an insufficient number of parameters were provided. Specify Automatic for automatic trim. Specify DeleteBeforeDays for delete older than days. Specify MajorVersionLimit and MajorWithMinorVersionsLimit for version count limits."); + } + if (Force || ShouldContinue("By executing this command, versions specified will be permanently deleted. These versions cannot be restored from the recycle bin. Are you sure you want to continue?", Resources.Confirm)) { var list = Identity.GetList(CurrentWeb); if (list != null) { - list.StartDeleteFileVersions(DeleteBeforeDays); + var ps = new FileVersionBatchDeleteParameters(); + + ps.BatchDeleteMode = batchDeleteMode; + ps.DeleteOlderThanDays = DeleteBeforeDays; + ps.MajorVersionLimit = MajorVersionLimit; + ps.MajorWithMinorVersionsLimit = MajorWithMinorVersionsLimit; + + list.StartDeleteFileVersionsByMode(ps); + ClientContext.ExecuteQueryRetry(); WriteObject("Success. Versions specified will be permanently deleted in the upcoming days."); diff --git a/src/Commands/Model/SharePoint/FileVersionBatchDeleteJobProgress.cs b/src/Commands/Model/SharePoint/FileVersionBatchDeleteJobProgress.cs new file mode 100644 index 000000000..49a29853a --- /dev/null +++ b/src/Commands/Model/SharePoint/FileVersionBatchDeleteJobProgress.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace PnP.PowerShell.Commands.Model.SharePoint +{ + /// + /// The progress for the request that batch delete file versions + /// + public class FileVersionBatchDeleteJobProgress + { + /// + /// Site/Library Url + /// + public string Url { get; set; } + + /// + /// The workitem Id related to the request + /// + [JsonPropertyName("WorkItemId")] + public string WorkItemId { get; set; } + + /// + /// The request status + /// + [JsonPropertyName("Status")] + public string Status { get; set; } + + /// + /// The UTC time user sent the request + /// + [JsonPropertyName("RequestTimeInUTC")] + public string RequestTimeInUTC { get; set; } + + /// + /// The UTC time the server last processed the request + /// + [JsonPropertyName("LastProcessTimeInUTC")] + public string LastProcessTimeInUTC { get; set; } + + /// + /// The UTC time the request completes + /// + [JsonPropertyName("CompleteTimeInUTC")] + public string CompleteTimeInUTC { get; set; } + + /// + /// The batch delete mode + /// + [JsonPropertyName("BatchDeleteMode")] + public string BatchDeleteMode { get; set; } + + /// + /// Delete older than when BatchDeleteMode is DeleteOlderThanDays + /// + [JsonPropertyName("DeleteOlderThan")] + public string DeleteOlderThan { get; set; } + + /// + /// MajorVersionLimit when BatchDeleteMode is CountLimits + /// + [JsonPropertyName("MajorVersionLimit")] + public string MajorVersionLimit { get; set; } + + /// + /// MajorWithMinorVersionsLimit when BatchDeleteMode is CountLimits + /// + [JsonPropertyName("MajorWithMinorVersionsLimit")] + public string MajorWithMinorVersionsLimit { get; set; } + + /// + /// The files processed count + /// + [JsonPropertyName("FilesProcessed")] + public string FilesProcessed { get; set; } + + /// + /// The versions failed to process count + /// + [JsonPropertyName("VersionsProcessed")] + public string VersionsProcessed { get; set; } + + /// + /// Set versions deleted count + /// + [JsonPropertyName("VersionsDeleted")] + public string VersionsDeleted { get; set; } + + /// + /// The versions failed to delete count + /// + [JsonPropertyName("VersionsFailed")] + public string VersionsFailed { get; set; } + + /// + /// The storage released + /// + [JsonPropertyName("StorageReleased")] + public string StorageReleasedInBytes { get; set; } + } +} diff --git a/src/Commands/Model/SharePoint/SetVersionPolicyProgress.cs b/src/Commands/Model/SharePoint/SetVersionPolicyProgress.cs index 0bb6d3f01..f587ca6c3 100644 --- a/src/Commands/Model/SharePoint/SetVersionPolicyProgress.cs +++ b/src/Commands/Model/SharePoint/SetVersionPolicyProgress.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace PnP.PowerShell.Commands.Model.SharePoint @@ -19,57 +20,68 @@ public class SetVersionPolicyProgress /// /// The workitem Id related to the request /// + [JsonPropertyName("WorkItemId")] public string WorkItemId { get; set; } /// /// The request status /// + [JsonPropertyName("Status")] public string Status { get; set; } /// /// The UTC time user sent the request /// + [JsonPropertyName("RequestTimeInUTC")] public string RequestTimeInUTC { get; set; } /// /// The UTC time the server last processed the request /// + [JsonPropertyName("LastProcessTimeInUTC")] public string LastProcessTimeInUTC { get; set; } /// /// The UTC time the request completes /// + [JsonPropertyName("CompleteTimeInUTC")] public string CompleteTimeInUTC { get; set; } /// /// The lists processed count /// - public string ListsProcessedInTotal { get; set; } + [JsonPropertyName("ListsProcessedInTotal")] + public string LibrariesProcessedInTotal { get; set; } /// /// The lists failed to process count /// - public string ListsFailedInTotal { get; set; } + [JsonPropertyName("ListsFailedInTotal")] + public string LibrariesFailedInTotal { get; set; } /// /// Set version policy as AutoExpiration or not /// - public string EnableAutoTrim { get; set; } + [JsonPropertyName("EnableAutoTrim")] + public string EnableAutomaticMode{ get; set; } /// /// The time limit if the version policy is ExpireAfter /// + [JsonPropertyName("ExpireAfterDays")] public string ExpireAfterDays { get; set; } /// /// MajorVersionLimit for the versions /// + [JsonPropertyName("MajorVersionLimit")] public string MajorVersionLimit { get; set; } /// /// MajorWithMinorVersionsLimit for the versions /// if minor version is enabled /// + [JsonPropertyName("MajorWithMinorVersionsLimit")] public string MajorWithMinorVersionsLimit { get; set; } } } diff --git a/src/Commands/Model/SharePoint/SetVersionPolicyProgressClient.cs b/src/Commands/Model/SharePoint/SetVersionPolicyProgressClient.cs deleted file mode 100644 index d5108b016..000000000 --- a/src/Commands/Model/SharePoint/SetVersionPolicyProgressClient.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PnP.PowerShell.Commands.Model.SharePoint -{ - /// - /// The progress for the request that settting version policy for existing document libraries of the site - /// - public class SetVersionPolicyProgressClient - { - /// - /// Site Url - /// - public string Url { get; set; } - - /// - /// The workitem Id related to the request - /// - public string WorkItemId { get; set; } - - /// - /// The request status - /// - public string Status { get; set; } - - /// - /// The UTC time user sent the request - /// - public string RequestTimeInUTC { get; set; } - - /// - /// The UTC time the server last processed the request - /// - public string LastProcessTimeInUTC { get; set; } - - /// - /// The UTC time the request completes - /// - public string CompleteTimeInUTC { get; set; } - - /// - /// The Libraries processed count - /// - public string LibrariesProcessedInTotal { get; set; } - - /// - /// The Libraries failed to process count - /// - public string LibrariesFailedInTotal { get; set; } - - /// - /// Set version policy as AutoExpiration or not - /// - public string EnableAutomaticMode { get; set; } - - /// - /// The time limit if the version policy is ExpireAfter - /// - public string ExpireAfterDays { get; set; } - - /// - /// MajorVersionLimit for the versions - /// - public string MajorVersionLimit { get; set; } - - /// - /// MajorWithMinorVersionsLimit for the versions - /// if minor version is enabled - /// - public string MajorWithMinorVersionsLimit { get; set; } - } -} diff --git a/src/Commands/Site/GetSetVersionPolicyProgress.cs b/src/Commands/Site/GetSetVersionPolicyProgress.cs index c8eb1477c..56987b645 100644 --- a/src/Commands/Site/GetSetVersionPolicyProgress.cs +++ b/src/Commands/Site/GetSetVersionPolicyProgress.cs @@ -9,7 +9,7 @@ namespace PnP.PowerShell.Commands.Site { [Cmdlet(VerbsCommon.Get, "PnPSiteVersionPolicyProgress")] - [OutputType(typeof(PnP.PowerShell.Commands.Model.SharePoint.SetVersionPolicyProgressClient))] + [OutputType(typeof(PnP.PowerShell.Commands.Model.SharePoint.SetVersionPolicyProgress))] public class GetSetVersionPolicyProgress : PnPSharePointCmdlet { protected override void ExecuteCmdlet() @@ -32,24 +32,7 @@ protected override void ExecuteCmdlet() progress.CompleteTimeInUTC = string.Empty; } - // Display different property names in the results using Client object - var progressClient = new SetVersionPolicyProgressClient() - { - Url = progress.Url, - WorkItemId = progress.WorkItemId, - Status = progress.Status, - RequestTimeInUTC = progress.RequestTimeInUTC, - LastProcessTimeInUTC = progress.LastProcessTimeInUTC, - CompleteTimeInUTC = progress.CompleteTimeInUTC, - LibrariesProcessedInTotal = progress.ListsProcessedInTotal, - LibrariesFailedInTotal = progress.ListsFailedInTotal, - EnableAutomaticMode = progress.EnableAutoTrim, - ExpireAfterDays = progress.ExpireAfterDays, - MajorVersionLimit = progress.MajorVersionLimit, - MajorWithMinorVersionsLimit = progress.MajorWithMinorVersionsLimit - }; - - WriteObject(progressClient); + WriteObject(progress); } } } diff --git a/src/Commands/Site/GetSiteFileVersionBatchDeleteJobProgress.cs b/src/Commands/Site/GetSiteFileVersionBatchDeleteJobProgress.cs new file mode 100644 index 000000000..95472a6bd --- /dev/null +++ b/src/Commands/Site/GetSiteFileVersionBatchDeleteJobProgress.cs @@ -0,0 +1,50 @@ +using Microsoft.SharePoint.Client; + +using PnP.Framework.Utilities; +using PnP.PowerShell.Commands.Base.PipeBinds; +using PnP.PowerShell.Commands.Model.SharePoint; +using System; +using System.Management.Automation; +using System.Text.Json; + +namespace PnP.PowerShell.Commands.Sites +{ + [Cmdlet(VerbsCommon.Get, "PnPSiteFileVersionBatchDeleteJobProgress")] + [OutputType(typeof(FileVersionBatchDeleteJobProgress))] + public class GetSiteFileVersionBatchDeleteJobProgress : PnPSharePointCmdlet + { + protected override void ExecuteCmdlet() + { + var site = ClientContext.Site; + ClientContext.Load(site, s => s.Url); + var ret = site.GetProgressForDeleteFileVersions(); + ClientContext.ExecuteQueryRetry(); + + var progress = JsonSerializer.Deserialize(ret.Value); + progress.Url = site.Url; + + if (!string.Equals(progress.BatchDeleteMode, FileVersionBatchDeleteMode.DeleteOlderThanDays.ToString(), StringComparison.OrdinalIgnoreCase)) + { + progress.DeleteOlderThan = string.Empty; + } + + if (!string.Equals(progress.BatchDeleteMode, FileVersionBatchDeleteMode.CountLimits.ToString(), StringComparison.OrdinalIgnoreCase)) + { + progress.MajorVersionLimit = string.Empty; + progress.MajorWithMinorVersionsLimit = string.Empty; + } + + if (string.Equals(progress.LastProcessTimeInUTC, DateTime.MinValue.ToString())) + { + progress.LastProcessTimeInUTC = string.Empty; + } + + if (string.Equals(progress.CompleteTimeInUTC, DateTime.MinValue.ToString())) + { + progress.CompleteTimeInUTC = string.Empty; + } + + WriteObject(progress); + } + } +} diff --git a/src/Commands/Site/NewSiteFileVersionBatchDeleteJob.cs b/src/Commands/Site/NewSiteFileVersionBatchDeleteJob.cs index 6da8d9c06..06ee8b940 100644 --- a/src/Commands/Site/NewSiteFileVersionBatchDeleteJob.cs +++ b/src/Commands/Site/NewSiteFileVersionBatchDeleteJob.cs @@ -12,18 +12,63 @@ namespace PnP.PowerShell.Commands.Sites [Cmdlet(VerbsCommon.New, "PnPSiteFileVersionBatchDeleteJob")] public class NewSiteFileVersionBatchDeleteJob : PnPSharePointCmdlet { - [Parameter(Mandatory = true)] + private const string ParameterSet_AUTOMATICTRIM = "AutomaticTrim"; + private const string ParameterSet_DELETEOLDERTHANDAYS = "DeleteOlderThanDays"; + private const string ParameterSet_COUNTLIMITS = "CountLimits"; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_AUTOMATICTRIM)] + public SwitchParameter Automatic; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_DELETEOLDERTHANDAYS)] public int DeleteBeforeDays; + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_COUNTLIMITS)] + public int MajorVersionLimit; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_COUNTLIMITS)] + public int MajorWithMinorVersionsLimit; + [Parameter(Mandatory = false)] public SwitchParameter Force; protected override void ExecuteCmdlet() { + FileVersionBatchDeleteMode batchDeleteMode; + if (Automatic) + { + batchDeleteMode = FileVersionBatchDeleteMode.AutomaticTrim; + DeleteBeforeDays = -1; + MajorVersionLimit = -1; + MajorWithMinorVersionsLimit = -1; + } + else if (ParameterSpecified(nameof(DeleteBeforeDays))) + { + batchDeleteMode = FileVersionBatchDeleteMode.DeleteOlderThanDays; + MajorVersionLimit = -1; + MajorWithMinorVersionsLimit = -1; + } + else if (ParameterSpecified(nameof(MajorVersionLimit)) || + ParameterSpecified(nameof(MajorWithMinorVersionsLimit))) + { + batchDeleteMode = FileVersionBatchDeleteMode.CountLimits; + DeleteBeforeDays = -1; + } + else + { + throw new PSArgumentException($"One or more parameters issued cannot be used together or an insufficient number of parameters were provided. Specify Automatic for automatic trim. Specify DeleteBeforeDays for delete older than days. Specify MajorVersionLimit and MajorWithMinorVersionsLimit for version count limits."); + } + if (Force || ShouldContinue("By executing this command, versions specified will be permanently deleted. These versions cannot be restored from the recycle bin. Are you sure you want to continue?", Resources.Confirm)) { var site = ClientContext.Site; - site.StartDeleteFileVersions(DeleteBeforeDays); + var ps = new FileVersionBatchDeleteParameters(); + + ps.BatchDeleteMode = batchDeleteMode; + ps.DeleteOlderThanDays = DeleteBeforeDays; + ps.MajorVersionLimit = MajorVersionLimit; + ps.MajorWithMinorVersionsLimit = MajorWithMinorVersionsLimit; + + site.StartDeleteFileVersionsByMode(ps); ClientContext.ExecuteQueryRetry(); WriteObject("Success. Versions specified will be permanently deleted in the upcoming days.");