Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new versioning cmdlets. #3799

Merged
merged 21 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Commands/Files/GetFileVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class GetFileVersion : PnPWebCmdlet
[Parameter(Mandatory = true)]
public string Url;

[Parameter(Mandatory = false)]
public SwitchParameter UseVersionExpirationReport;
blarrywangmsft marked this conversation as resolved.
Show resolved Hide resolved

protected override void ExecuteCmdlet()
{
var serverRelativeUrl = string.Empty;
Expand All @@ -31,13 +34,21 @@ protected override void ExecuteCmdlet()
File file;

file = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));

if (UseVersionExpirationReport)
{
ClientContext.Load(file, f => f.Exists, f => f.VersionExpirationReport.IncludeWithDefaultProperties(i => i.CreatedBy, i => i.SnapshotDate, i => i.ExpirationDate));
}
else
{
ClientContext.Load(file, f => f.Exists, f => f.Versions.IncludeWithDefaultProperties(i => i.CreatedBy, i => i.SnapshotDate, i => i.ExpirationDate));
}

ClientContext.Load(file, f => f.Exists, f => f.Versions.IncludeWithDefaultProperties(i => i.CreatedBy));
ClientContext.ExecuteQueryRetry();

if (file.Exists)
{
var versions = file.Versions;
var versions = UseVersionExpirationReport ? file.VersionExpirationReport : file.Versions;
ClientContext.ExecuteQueryRetry();
WriteObject(versions, true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.SharePoint.Client;

using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Model.SharePoint;

using System.Management.Automation;
using System.Text.Json;

namespace PnP.PowerShell.Commands.Lists
{
[Cmdlet(VerbsCommon.Get, "LibraryLevelFileVersionExpirationReportJobProgress")]
[OutputType(typeof(FileVersionExpirationReportJobProgress))]
public class GetLibraryLevelFileVersionExpirationReportJobProgress : PnPWebCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)]
[ValidateNotNull]
public ListPipeBind Identity;

[Parameter(Mandatory = true)]
public string ReportUrl;

protected override void ExecuteCmdlet()
{
var list = Identity.GetList(CurrentWeb);
if (list != null)
{
var ret = list.GetProgressForFileVersionExpirationReport(ReportUrl);
ClientContext.ExecuteQueryRetry();

var status = JsonSerializer.Deserialize<FileVersionExpirationReportJobProgress>(ret.Value);
status.Url = list.RootFolder.ServerRelativeUrl;
status.ReportUrl = ReportUrl;

WriteObject(status);
}
}
}
}
44 changes: 44 additions & 0 deletions src/Commands/Lists/NewLibraryLevelFileVersionBatchDeleteJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.SharePoint.Client;

using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;

using System.Management.Automation;

using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.Lists
{
[Cmdlet(VerbsCommon.New, "LibraryLevelFileVersionBatchDeleteJob")]
public class NewLibraryLevelFileVersionBatchDeleteJob : PnPWebCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)]
[ValidateNotNull]
public ListPipeBind Identity;

[Parameter(Mandatory = true)]
public int DeleteBeforeDays;

[Parameter(Mandatory = false)]
public SwitchParameter Force;

protected override void ExecuteCmdlet()
{
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);
ClientContext.ExecuteQueryRetry();

WriteVerbose("Success. Versions specified will be permanently deleted in the upcoming days.");
}
}
else
{
WriteVerbose("Cancelled. No versions will be deleted.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.SharePoint.Client;

using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;

using System.Management.Automation;

namespace PnP.PowerShell.Commands.Lists
{
[Cmdlet(VerbsCommon.New, "LibraryLevelFileVersionExpirationReportJob")]
public class NewLibraryLevelFileVersionExpirationReportJob : PnPWebCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)]
[ValidateNotNull]
public ListPipeBind Identity;

[Parameter(Mandatory = true)]
public string ReportUrl;

protected override void ExecuteCmdlet()
{
var list = Identity.GetList(CurrentWeb);
if (list != null)
{
list.StartFileVersionExpirationReport(ReportUrl);
ClientContext.ExecuteQueryRetry();

WriteVerbose("Success. The file version expiration report will be gradually populated. It will take over 24 hours to complete for a small library, and a few days for a larger one.");
blarrywangmsft marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
41 changes: 41 additions & 0 deletions src/Commands/Lists/RemoveLibraryLevelFileVersionBatchDeleteJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.SharePoint.Client;

using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;

using System.Management.Automation;

using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.Lists
{
[Cmdlet(VerbsCommon.Remove, "LibraryLevelFileVersionBatchDeleteJob")]
public class RemoveLibraryLevelFileVersionBatchDeleteJob : PnPWebCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)]
[ValidateNotNull]
public ListPipeBind Identity;

[Parameter(Mandatory = false)]
public SwitchParameter Force;

protected override void ExecuteCmdlet()
{
if (Force || ShouldContinue("It will stop processing further version deletion batches. Are you sure you want to continue?", Resources.Confirm))
{
var list = Identity.GetList(CurrentWeb);
if (list != null)
{
list.CancelDeleteFileVersions();
ClientContext.ExecuteQueryRetry();
}

WriteVerbose("Future deletion is successfully stopped.");
}
else
{
WriteVerbose("Did not receive confirmation to stop deletion. Continuing to delete specified versions.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text.Json.Serialization;

namespace PnP.PowerShell.Commands.Model.SharePoint
{
/// <summary>
/// The progress for generating the file version expiration report.
/// </summary>
public class FileVersionExpirationReportJobProgress
{
/// <summary>
/// The URL to the site or library that the report is for
/// </summary>
[JsonPropertyName("url")]
public string Url { get; set; }

/// <summary>
/// The URL of the report file
/// </summary>
[JsonPropertyName("report_url")]
public string ReportUrl { get; set; }

/// <summary>
/// The request status
/// </summary>
[JsonPropertyName("status")]
public string Status { get; set; }

/// <summary>
/// The error message if any
/// </summary>
[JsonPropertyName( "error_message")]
public string ErrorMessage { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.SharePoint.Client;

using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Model.SharePoint;

using System.Management.Automation;
using System.Text.Json;

namespace PnP.PowerShell.Commands.Sites
{
[Cmdlet(VerbsCommon.Get, "SiteLevelFileVersionExpirationReportJobProgress")]
[OutputType(typeof(FileVersionExpirationReportJobProgress))]
public class GetSiteLevelFileVersionExpirationReportJobProgress : PnPSharePointCmdlet
{
[Parameter(Mandatory = true)]
public string ReportUrl;

protected override void ExecuteCmdlet()
{
var site = ClientContext.Site;
ClientContext.Load(site, s => s.Url);
var ret = site.GetProgressForFileVersionExpirationReport(ReportUrl);
ClientContext.ExecuteQueryRetry();

var status = JsonSerializer.Deserialize<FileVersionExpirationReportJobProgress>(ret.Value);
status.Url = site.Url;
status.ReportUrl = ReportUrl;

WriteObject(status);
}
}
}
37 changes: 37 additions & 0 deletions src/Commands/Site/NewSiteLevelFileVersionBatchDeleteJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.SharePoint.Client;

using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;

using System.Management.Automation;

using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.Sites
{
[Cmdlet(VerbsCommon.New, "SiteLevelFileVersionBatchDeleteJob")]
public class NewSiteLevelFileVersionBatchDeleteJob : PnPSharePointCmdlet
{
[Parameter(Mandatory = true)]
public int DeleteBeforeDays;

[Parameter(Mandatory = false)]
public SwitchParameter Force;

protected override void ExecuteCmdlet()
{
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);
blarrywangmsft marked this conversation as resolved.
Show resolved Hide resolved
ClientContext.ExecuteQueryRetry();

WriteVerbose("Success. Versions specified will be permanently deleted in the upcoming days.");
}
else
{
WriteVerbose("Cancelled. No versions will be deleted.");
}
}
}
}
25 changes: 25 additions & 0 deletions src/Commands/Site/NewSiteLevelFileVersionExpirationReportJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.SharePoint.Client;

using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;

using System.Management.Automation;

namespace PnP.PowerShell.Commands.Sites
{
[Cmdlet(VerbsCommon.New, "SiteLevelFileVersionExpirationReportJob")]
public class NewSiteLevelFileVersionExpirationReportJob : PnPSharePointCmdlet
{
[Parameter(Mandatory = true)]
public string ReportUrl;

protected override void ExecuteCmdlet()
{
var site = ClientContext.Site;
site.StartFileVersionExpirationReport(ReportUrl);
ClientContext.ExecuteQueryRetry();

WriteVerbose("Success. The file version expiration report will be gradually populated. It will take over 24 hours to complete for a small site, and a few days for a larger one.");
blarrywangmsft marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
34 changes: 34 additions & 0 deletions src/Commands/Site/RemoveSiteLevelFileVersionBatchDeleteJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.SharePoint.Client;

using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;

using System.Management.Automation;

using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.Sites
{
[Cmdlet(VerbsCommon.Remove, "SiteLevelFileVersionBatchDeleteJob")]
public class RemoveSiteLevelFileVersionBatchDeleteJob : PnPSharePointCmdlet
{
[Parameter(Mandatory = false)]
public SwitchParameter Force;

protected override void ExecuteCmdlet()
{
if (Force || ShouldContinue("It will stop processing further version deletion batches. Are you sure you want to continue?", Resources.Confirm))
{
var site = ClientContext.Site;
site.CancelDeleteFileVersions();
ClientContext.ExecuteQueryRetry();

WriteVerbose("Future deletion is successfully stopped.");
}
else
{
WriteVerbose("Did not receive confirmation to stop deletion. Continuing to delete specified versions.");
}
}
}
}