From 7f88ae6087b46921ba0d6c49fd788e0faaf66b3e Mon Sep 17 00:00:00 2001 From: Jenny Wu Date: Mon, 28 Aug 2023 16:38:08 -0700 Subject: [PATCH 1/2] [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 6ac4b9a3d..1d9fbe2c9 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 9e6043da0..98021f882 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); From ec289450330ff281cce50165c5384a3fc969a470 Mon Sep 17 00:00:00 2001 From: Koen Zomers Date: Sat, 2 Sep 2023 23:41:46 +0200 Subject: [PATCH 2/2] Added changelog entry --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dea4c4d15..0b321ed19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `-SharingStatus` parameter to `Get-PnPFlow` which allows for filtering flows based on their sharing status. [#3287](https://github.com/pnp/powershell/pull/3287) - Added `-AzureADLoginEndPoint` and `-MicrosoftGraphEndPoint` parameters to `Connect-PnPOnline` cmdlet for use in custom Azure environments. [#2925](https://github.com/pnp/powershell/pull/2925) - Added `SiteOwnerManageLegacyServicePrincipalEnabled` parameter to `Set-PnPTenant` cmdlet. With this parameter site owners will not be able to register/update apps unless the tenant admin explicitly allows it. [#3318](https://github.com/pnp/powershell/pull/3318) - +- Added `-EnableAutoExpirationVersionTrim`, `-ExpireVersionsAfterDays`, `-MajorVersions`, `-MinorVersions`, `-InheritTenantVersionPolicySettings`, `-StartApplyVersionPolicySettingToExistingDocLibs` and `-CancelApplyVersionPolicySettingToExistingDocLibs` to `Set-PnPSite` to allow for time based version expiration on the site level [#3373](https://github.com/pnp/powershell/pull/3373) + ### Fixed - Fixed `Add-PnPContentTypeToList` cmdlet to better handle piped lists. [#3244](https://github.com/pnp/powershell/pull/3244) @@ -45,6 +46,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Contributors +- [msjennywu] - Reshmee Auckloo [reshmee011] - Per Østergaard [per-oestergaard] - Nishkalank Bezawada [NishkalankBezawada]