From 24573e94518284cfc4a02972e2830fc352a9ca5b Mon Sep 17 00:00:00 2001 From: Jenny Wu Date: Mon, 28 Aug 2023 16:38:08 -0700 Subject: [PATCH] [FileVersion] Add set site version policy commands --- documentation/Set-PnPSite.md | 166 +++++++++++++++++++++++++++++++++++ src/Commands/Site/SetSite.cs | 140 +++++++++++++++++++++++++++++ 2 files changed, 306 insertions(+) diff --git a/documentation/Set-PnPSite.md b/documentation/Set-PnPSite.md index 6ac4b9a3d9..1d9fbe2c9c 100644 --- a/documentation/Set-PnPSite.md +++ b/documentation/Set-PnPSite.md @@ -47,6 +47,13 @@ Set-PnPSite [-Identity ] [-BlockDownloadPolicy ] [-ExcludeBlockDownloadPolicySiteOwners ] [-ExcludedBlockDownloadGroupIds ] [-ListsShowHeaderAndNavigation ] + [-EnableAutoExpirationVersionTrim ] + [-ExpireVersionsAfterDays ] + [-MajorVersions ] + [-MinorVersions ] + [-InheritTenantVersionPolicySettings] + [-StartApplyVersionPolicySettingToExistingDocLibs] + [-CancelApplyVersionPolicySettingToExistingDocLibs] [-Connection ] ``` @@ -103,6 +110,62 @@ Set-PnPSite -NoScriptSite $false Allows custom script on a specific site. See [Allow or prevent custom script](https://learn.microsoft.com/sharepoint/allow-or-prevent-custom-script) for more information. +### EXAMPLE 7 +```powershell +Set-PnPSite -EnableAutoExpirationVersionTrim $true +``` + +Set AutoExpiration file version trim mode for a site. The new document libraries will use this version setting. + +### EXAMPLE 8 +```powershell +Set-PnPSite -EnableAutoExpirationVersionTrim $false -MajorVersions 100 -ExpireVersionsAfterDays 200 +``` + +Set ExpireAfter file version trim mode for a site. The new document libraries will use this version setting. + +### EXAMPLE 9 +```powershell +Set-PnPSite -EnableAutoExpirationVersionTrim $false -MajorVersions 300 -ExpireVersionsAfterDays 0 +``` + +Set NoExpiration file version trim mode for a site. The new document libraries will use this version setting. + +### EXAMPLE 10 +```powershell +Set-PnPSite -InheritTenantVersionPolicySettings +``` + +Clear the file version setting on a site. The new document libraries will use the tenant level setting. + +### EXAMPLE 11 +```powershell +Set-PnPSite -EnableAutoExpirationVersionTrim $true -StartApplyVersionPolicySettingToExistingDocLibs +``` + +Create a request to set the file version trim mode as AutoExpiration for existing document libraries that enabled versioning. + +### EXAMPLE 12 +```powershell +Set-PnPSite -EnableAutoExpirationVersionTrim $false -MajorVersions 100 -MinorVersions 5 -ExpireVersionsAfterDays 200 -StartApplyVersionPolicySettingToExistingDocLibs +``` + +Create a request to set the file version trim mode as ExpireAfter for existing document libraries that enabled versioning. + +### EXAMPLE 13 +```powershell +Set-PnPSite -EnableAutoExpirationVersionTrim $false -MajorVersions 100 -MinorVersions 5 -ExpireVersionsAfterDays 0 -StartApplyVersionPolicySettingToExistingDocLibs +``` + +Create a request to set the file version trim mode as NoExpiration for existing document libraries that enabled versioning. + +### EXAMPLE 14 +```powershell +Set-PnPSite -CancelApplyVersionPolicySettingToExistingDocLibs +``` + +Cancel the existing request which sets the file version trim mode for existing document libraries on a site. + ## PARAMETERS ### -AllowSelfServiceUpgrade @@ -613,6 +676,109 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -EnableAutoExpirationVersionTrim +Enable or disable AutoExpiration version trim for the document libraries on the site. Set to $true to enable, $false to disable. + +Parameter ExpireVersionsAfterDays is required when EnableAutoExpirationVersionTrim is false. Set it to 0 for NoExpiration, set it to greater or equal to 30 for ExpireAfter. + +Parameter MajorVersions is required when EnableAutoExpirationVersionTrim is false. +Parameter MinorVersions is required when EnableAutoExpirationVersionTrim is false and StartApplyVersionPolicySettingToExistingDocLibs is specified. It is used when minor version is enabled on the document libraries. + +```yaml +Type: Boolean +Parameter Sets: Set Properties + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -ExpireVersionsAfterDays +Work with parameter EnableAutoExpirationVersionTrim. Please see description in EnableAutoExpirationVersionTrim. + +```yaml +Type: UInt32 +Parameter Sets: Set Properties + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -MajorVersions +Work with parameter EnableAutoExpirationVersionTrim. Please see description in EnableAutoExpirationVersionTrim. + +```yaml +Type: UInt32 +Parameter Sets: Set Properties + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -MinorVersions +Work with parameter EnableAutoExpirationVersionTrim and StartApplyVersionPolicySettingToExistingDocLibs. Please see description in EnableAutoExpirationVersionTrim. + +```yaml +Type: UInt32 +Parameter Sets: Set Properties + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -InheritTenantVersionPolicySettings +Clear the file version setting on a site. The new document libraries will use the tenant level setting. + +```yaml +Type: SwitchParameter +Parameter Sets: Set Properties + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -StartApplyVersionPolicySettingToExistingDocLibs +Create a request to set the file version trim mode for existing document libraries that enabled versioning. Work with parameters EnableAutoExpirationVersionTrim, ExpireVersionsAfterDays, MajorVersions and MinorVersions. + +```yaml +Type: SwitchParameter +Parameter Sets: Set Properties + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -CancelApplyVersionPolicySettingToExistingDocLibs +Cancel the existing request which sets the file version trim mode for existing document libraries on a site. + +```yaml +Type: SwitchParameter +Parameter Sets: Set Properties + +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/src/Commands/Site/SetSite.cs b/src/Commands/Site/SetSite.cs index 9e6043da05..98021f8826 100644 --- a/src/Commands/Site/SetSite.cs +++ b/src/Commands/Site/SetSite.cs @@ -127,6 +127,27 @@ public class SetSite : PnPSharePointCmdlet [Parameter(Mandatory = false, ParameterSetName = ParameterSet_LOCKSTATE)] public SwitchParameter Wait; + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_PROPERTIES)] + public bool EnableAutoExpirationVersionTrim; + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_PROPERTIES)] + public int ExpireVersionsAfterDays; + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_PROPERTIES)] + public int MajorVersions; + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_PROPERTIES)] + public int MinorVersions; + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_PROPERTIES)] + public SwitchParameter InheritTenantVersionPolicySettings; + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_PROPERTIES)] + public SwitchParameter StartApplyVersionPolicySettingToExistingDocLibs; + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_PROPERTIES)] + public SwitchParameter CancelApplyVersionPolicySettingToExistingDocLibs; + protected override void ExecuteCmdlet() { var context = ClientContext; @@ -235,6 +256,125 @@ protected override void ExecuteCmdlet() } } + if (ParameterSpecified(nameof(InheritTenantVersionPolicySettings))) + { + if (ParameterSpecified(nameof(EnableAutoExpirationVersionTrim)) || + ParameterSpecified(nameof(ExpireVersionsAfterDays)) || + ParameterSpecified(nameof(MajorVersions)) || + ParameterSpecified(nameof(MinorVersions)) || + ParameterSpecified(nameof(StartApplyVersionPolicySettingToExistingDocLibs)) || + ParameterSpecified(nameof(CancelApplyVersionPolicySettingToExistingDocLibs))) + { + throw new PSArgumentException($"The VersionPolicy related parameters (EnableAutoExpirationVersionTrim, ExpireVersionsAfterDays, MajorVersions, StartApplyVersionPolicySettingToExistingDocLibs, CancelApplyVersionPolicySettingToExistingDocLibs) cannot be set when InheritTenantVersionPolicySettings is specified."); + } + + site.EnsureProperty(s => s.VersionPolicyForNewLibrariesTemplate); + site.VersionPolicyForNewLibrariesTemplate.InheritTenantSettings(); + context.ExecuteQueryRetry(); + } + else + { + if (ParameterSpecified(nameof(StartApplyVersionPolicySettingToExistingDocLibs)) && + ParameterSpecified(nameof(CancelApplyVersionPolicySettingToExistingDocLibs))) + { + throw new PSArgumentException($"Don't specify both StartApplyVersionPolicySettingToExistingDocLibs and CancelApplyVersionPolicySettingToExistingDocLibs."); + } + + if (ParameterSpecified(nameof(CancelApplyVersionPolicySettingToExistingDocLibs))) + { + if (ParameterSpecified(nameof(EnableAutoExpirationVersionTrim)) || + ParameterSpecified(nameof(ExpireVersionsAfterDays)) || + ParameterSpecified(nameof(MajorVersions)) || + ParameterSpecified(nameof(MinorVersions))) + { + throw new PSArgumentException($"The VersionPolicy related parameters (EnableAutoExpirationVersionTrim, ExpireVersionsAfterDays, MajorVersions) cannot be set when CancelApplyVersionPolicySettingToExistingDocLibs is specified."); + } + + site.CancelSetVersionPolicyForDocLibs(); + context.ExecuteQueryRetry(); + } + else + { + if (ParameterSpecified(nameof(StartApplyVersionPolicySettingToExistingDocLibs))) + { + if (ParameterSpecified(nameof(EnableAutoExpirationVersionTrim))) + { + if (EnableAutoExpirationVersionTrim) + { + if (ParameterSpecified(nameof(ExpireVersionsAfterDays)) || + ParameterSpecified(nameof(MajorVersions)) || + ParameterSpecified(nameof(MinorVersions))) + { + throw new PSArgumentException($"Don't specify ExpireVersionsAfterDays, MajorVersions and MinorVersions when EnableAutoExpirationVersionTrim is true."); + } + + site.StartSetVersionPolicyForDocLibs(true, -1, -1, -1); + context.ExecuteQueryRetry(); + } + else + { + if (!ParameterSpecified(nameof(ExpireVersionsAfterDays)) || + !ParameterSpecified(nameof(MajorVersions)) || + !ParameterSpecified(nameof(MinorVersions))) + { + throw new PSArgumentException($"You must specify ExpireVersionsAfterDays, MajorVersions and MinorVersions when EnableAutoExpirationVersionTrim is false."); + } + + site.StartSetVersionPolicyForDocLibs(false, MajorVersions, MinorVersions, ExpireVersionsAfterDays); + context.ExecuteQueryRetry(); + } + } + else + { + throw new PSArgumentException($"You must specify EnableAutoExpirationVersionTrim when StartApplyVersionPolicySettingToExistingDocLibs is specified."); + } + } + else + { + if (ParameterSpecified(nameof(EnableAutoExpirationVersionTrim))) + { + if (ParameterSpecified(nameof(MinorVersions))) + { + throw new PSArgumentException($"Don't specify MinorVersions when StartApplyVersionPolicySettingToExistingDocLibs is not specified."); + } + + if (EnableAutoExpirationVersionTrim) + { + if (ParameterSpecified(nameof(ExpireVersionsAfterDays)) || + ParameterSpecified(nameof(MajorVersions))) + { + throw new PSArgumentException($"Don't specify ExpireVersionsAfterDays and MajorVersions when EnableAutoExpirationVersionTrim is true."); + } + + site.EnsureProperty(s => s.VersionPolicyForNewLibrariesTemplate); + site.VersionPolicyForNewLibrariesTemplate.SetAutoExpiration(); + context.ExecuteQueryRetry(); + } + else + { + if (!ParameterSpecified(nameof(ExpireVersionsAfterDays)) || + !ParameterSpecified(nameof(MajorVersions))) + { + throw new PSArgumentException($"You must specify ExpireVersionsAfterDays and MajorVersions when EnableAutoExpirationVersionTrim is false."); + } + + site.EnsureProperty(s => s.VersionPolicyForNewLibrariesTemplate); + if (ExpireVersionsAfterDays == 0) + { + site.VersionPolicyForNewLibrariesTemplate.SetNoExpiration(MajorVersions); + context.ExecuteQueryRetry(); + } + else + { + site.VersionPolicyForNewLibrariesTemplate.SetExpireAfter(MajorVersions, ExpireVersionsAfterDays); + context.ExecuteQueryRetry(); + } + } + } + } + } + } + if (IsTenantProperty()) { var tenantAdminUrl = UrlUtilities.GetTenantAdministrationUrl(context.Url);