Skip to content

Commit

Permalink
[FileVersion] Add set site version policy commands
Browse files Browse the repository at this point in the history
  • Loading branch information
msjennywu authored and KoenZomers committed Sep 2, 2023
1 parent 21aece0 commit 24573e9
Show file tree
Hide file tree
Showing 2 changed files with 306 additions and 0 deletions.
166 changes: 166 additions & 0 deletions documentation/Set-PnPSite.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ Set-PnPSite [-Identity <String>]
[-BlockDownloadPolicy <Boolean>] [-ExcludeBlockDownloadPolicySiteOwners <Boolean>]
[-ExcludedBlockDownloadGroupIds <Guid[]>]
[-ListsShowHeaderAndNavigation <Boolean>]
[-EnableAutoExpirationVersionTrim <Boolean>]
[-ExpireVersionsAfterDays <UInt32>]
[-MajorVersions <UInt32>]
[-MinorVersions <UInt32>]
[-InheritTenantVersionPolicySettings]
[-StartApplyVersionPolicySettingToExistingDocLibs]
[-CancelApplyVersionPolicySettingToExistingDocLibs]
[-Connection <PnPConnection>]
```

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
140 changes: 140 additions & 0 deletions src/Commands/Site/SetSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 24573e9

Please sign in to comment.