Skip to content

Commit 8e6cec2

Browse files
authored
Merge pull request #4457 from reshmee011/unlockrecord
New cmdlet to update retention label on file
2 parents 452a8d4 + 70b41d1 commit 8e6cec2

File tree

4 files changed

+188
-2
lines changed

4 files changed

+188
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
5050
- Added `Get-PnPFileRetentionLabel` cmdlet to fetch the file retention labels. [#4603](https://github.com/pnp/powershell/pull/4603)
5151
- Added `Get/Set/Remove-PnPUserProfilePhoto` cmdlets to download, upload or remove the profile photo of the specified user.
5252
- Added `New/Get/Remove/Update-PnPTodoList` cmdlets to manage Todo lists.
53+
- Added `Set-PnPFileRetentionLabel` which allows setting a retention label on a file in SharePoint or locking/unlocking it. [#4457](https://github.com/pnp/powershell/pull/4457)
5354
- Added `Get-PnPFileCheckedOut` cmdlet to retrieve all files that are currently checked out in a library [#4682](https://github.com/pnp/powershell/pull/4682)
5455
- Added `Get-PnPTenantPronounsSetting` and `Set-PnPTenantPronounsSetting` cmdlets to manage the availability of using pronouns in the organization [#4660](https://github.com/pnp/powershell/pull/4660)
5556
- Added `HidePeopleWhoHaveListsOpen` parameter to `Set-PnPSite` cmdlet to hide people who simultaneously have lists open [#4699](https://github.com/pnp/powershell/pull/4699)

documentation/Get-PnPFileRetentionLabel.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ This example retrieves the retention label information for the file at the speci
3535
Get-PnPFileRetentionLabel -Url "/sites/Marketing/Shared Documents/Report.pptx"
3636
```
3737

38-
This example retrieves the retention label information for the file at the specified URL.
39-
4038
## PARAMETERS
4139

4240
### -Url
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
schema: 2.0.0
4+
applicable: SharePoint Online
5+
online version: https://pnp.github.io/powershell/cmdlets/Set-PnPFileRetentionLabel.html
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
title: Set-PnPFileRetentionLabel
8+
---
9+
10+
# Set-PnPFileRetentionLabel
11+
12+
## SYNOPSIS
13+
14+
**Required Permissions**
15+
16+
* Microsoft Graph API : One of Files.Read.All, Sites.Read.All, Files.ReadWrite.All, Sites.ReadWrite.All
17+
18+
Allows setting a retention label on a file in SharePoint or locking/unlocking it.
19+
20+
## SYNTAX
21+
22+
### Lock or unlock a file
23+
```powershell
24+
Set-PnPFileRetentionLabel -Identity <FilePipeBind> -RecordLocked <Boolean> [-Connection <PnPConnection>]
25+
```
26+
27+
### Set a retention label on a file
28+
```powershell
29+
Set-PnPFileRetentionLabel -Identity <FilePipeBind> -RetentionLabel <String> [-Connection <PnPConnection>]
30+
```
31+
32+
## DESCRIPTION
33+
34+
The Set-PnPFileRetentionLabel cmdlet updates the retention label information or locks/unlocks a file in SharePoint using Microsoft Graph. It takes a URL as input, decodes it, and specifically encodes the '+' character if it is part of the filename.
35+
36+
## EXAMPLES
37+
38+
### Example 1
39+
```powershell
40+
Set-PnPFileRetentionLabel -Url "/sites/Marketing/Shared Documents/Report.pptx" -RecordLocked $true
41+
```
42+
43+
This example locks the file at the specified URL.
44+
45+
### Example 2
46+
```powershell
47+
Set-PnPFileRetentionLabel -Identity "/sites/Marketing/Shared Documents/Report.pptx" -RetentionLabel "Finance"
48+
```
49+
50+
This example updates the retention label information for the file at the specified URL.
51+
52+
### Example 3
53+
```powershell
54+
Set-PnPFileRetentionLabel -Identity "/sites/Marketing/Shared Documents/Report.pptx" -RetentionLabel ""
55+
```
56+
57+
This example removes the retention label information from the file at the specified URL.
58+
59+
## PARAMETERS
60+
61+
### -Identity
62+
Specifies the server relative URL, File instance, listitem instance or Id of the file for which to set the retention label information or change the locking state.
63+
64+
```yaml
65+
Type: FilePipeBind
66+
Parameter Sets: (All)
67+
68+
Required: True
69+
Position: Named
70+
Default value: None
71+
Accept pipeline input: True
72+
Accept wildcard characters: False
73+
```
74+
75+
### -RecordLocked
76+
Specifies whether to lock or unlock the file. If omitted, the file is not locked or unlocked.
77+
78+
```yaml
79+
Type: Boolean
80+
Parameter Sets: Lock or unlock a file
81+
Required: True
82+
Position: Named
83+
Default value: None
84+
Accept pipeline input: True
85+
Accept wildcard characters: False
86+
```
87+
88+
### -RetentionLabel
89+
Specifies the retention label to apply to the file. Provide an empty string or $null to remove the existing label.
90+
91+
```yaml
92+
Type: String
93+
Parameter Sets: Set a retention label on a file
94+
Required: True
95+
Position: Named
96+
Default value: None
97+
Accept pipeline input: True
98+
Accept wildcard characters: False
99+
```
100+
101+
## RELATED LINKS
102+
103+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
104+
[Setting a retention label through Microsoft Graph](https://learn.microsoft.com/graph/api/driveitem-setretentionlabel)
105+
[Removing a retention label through Microsoft Graph](https://learn.microsoft.com/graph/api/driveitem-removeretentionlabel)
106+
[Locking or unlocking a file through Microsoft Graph](https://learn.microsoft.com/graph/api/driveitem-lockorunlockrecord)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Microsoft.SharePoint.Client;
2+
using PnP.Framework.Utilities;
3+
using PnP.PowerShell.Commands.Attributes;
4+
using PnP.PowerShell.Commands.Base;
5+
using PnP.PowerShell.Commands.Base.PipeBinds;
6+
using PnP.PowerShell.Commands.Model.Graph.Purview;
7+
using System;
8+
using System.Management.Automation;
9+
using System.Net.Http;
10+
using System.Text;
11+
using System.Text.Json;
12+
13+
namespace PnP.PowerShell.Commands.Files
14+
{
15+
[Cmdlet(VerbsCommon.Set, "PnPFileRetentionLabel", DefaultParameterSetName = ParameterSet_LOCKUNLOCK)]
16+
[RequiredApiDelegatedOrApplicationPermissions("graph/Files.Read.All")]
17+
[RequiredApiDelegatedOrApplicationPermissions("graph/Sites.Read.All")]
18+
[RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")]
19+
[RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")]
20+
[OutputType(typeof(FileRetentionLabel))]
21+
public class SetFileRetentionLabel : PnPGraphCmdlet
22+
{
23+
private const string ParameterSet_LOCKUNLOCK = "Lock or unlock a file";
24+
private const string ParameterSet_SETLABEL = "Set a retention label on a file";
25+
26+
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
27+
public FilePipeBind Identity;
28+
29+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SETLABEL)]
30+
public string RetentionLabel = string.Empty;
31+
32+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_LOCKUNLOCK)]
33+
public bool? RecordLocked;
34+
35+
protected override void ExecuteCmdlet()
36+
{
37+
var file = Identity.GetFile(ClientContext);
38+
file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID);
39+
40+
var requestUrl = $"v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/retentionLabel";
41+
42+
object payload = null;
43+
44+
switch(ParameterSetName)
45+
{
46+
case ParameterSet_LOCKUNLOCK:
47+
payload = new
48+
{
49+
retentionSettings = new
50+
{
51+
isRecordLocked = RecordLocked
52+
}
53+
};
54+
break;
55+
case ParameterSet_SETLABEL:
56+
if (string.IsNullOrEmpty(RetentionLabel))
57+
{
58+
WriteVerbose("Removing retention label");
59+
RequestHelper.Delete(requestUrl);
60+
}
61+
else
62+
{
63+
WriteVerbose($"Setting retention label to '{RetentionLabel}'");
64+
payload = new
65+
{
66+
name = RetentionLabel
67+
};
68+
}
69+
break;
70+
}
71+
72+
if (payload != null)
73+
{
74+
var jsonPayload = JsonSerializer.Serialize(payload);
75+
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
76+
var results = RequestHelper.Patch<FileRetentionLabel>(requestUrl, httpContent);
77+
WriteObject(results, true);
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)