Skip to content

Commit 477d8ee

Browse files
authored
Merge pull request #4538 from gautamdsheth/feature/3135
Add Add-PnPFileSensitivityLabel cmdlet for assigning sensitivity labels to SharePoint files
2 parents 6fb8c03 + 5b3c9e2 commit 477d8ee

File tree

5 files changed

+183
-17
lines changed

5 files changed

+183
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
3232
- Added `-HidePeoplePreviewingFiles` to `Set-PnPSite` which allows for hiding the people previewing files feature on a site [#4416](https://github.com/pnp/powershell/pull/4416)
3333
- Added `-AllowWebPropertyBagUpdateWhenDenyAddAndCustomizePagesIsEnabled` to `Set-PnPTenant` which allows for updating of web property bag when DenyAddAndCustomizePages is enabled [#4508](https://github.com/pnp/powershell/pull/4508)
3434
- Added `SiteId` to the output of `Get-PnPTenantSite` [#4527](https://github.com/pnp/powershell/pull/4527)
35+
- Added `Add-PnPFileSensitivityLabel` which allows for assigning sensitivity labels to SharePoint files [#4538](https://github.com/pnp/powershell/pull/4538)
3536

3637
### Changed
3738

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
schema: 2.0.0
4+
applicable: SharePoint Online
5+
online version: https://pnp.github.io/powershell/cmdlets/Add-PnPFileSensitivityLabel.html
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
title: Add-PnPFileSensitivityLabel
8+
---
9+
10+
# Add-PnPFileSensitivityLabel
11+
12+
## SYNOPSIS
13+
14+
**Required Permissions**
15+
16+
* Microsoft Graph API : One of Files.ReadWrite.All, Sites.ReadWrite.All
17+
18+
Add the sensitivity label information for a file in SharePoint.
19+
20+
## SYNTAX
21+
```powershell
22+
Add-PnPFileSensitivityLabel -Identity <String> -SensitivityLabelId <Guid> -AssignmentMethod <Enum> -JustificationText <string>
23+
```
24+
25+
## DESCRIPTION
26+
27+
The Add-PnPFileSensitivityLabel cmdlet adds the sensitivity label information for 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. It also takes the sensitivity label Id , assignment method and justification text values as input.
28+
29+
## EXAMPLES
30+
31+
### Example 1
32+
This example adds the sensitivity label information for the file at the specified URL.
33+
34+
```powershell
35+
Add-PnPFileSensitivityLabel -Identity "/sites/Marketing/Shared Documents/Report.pptx" -SensitivityLabelId "b5b11b04-05b3-4fe4-baa9-b7f5f65b8b64" -JustificationText "Previous label no longer applies" -AssignmentMethod Privileged
36+
```
37+
38+
### Example 2
39+
This example removes the sensitivity label information for the file at the specified URL.
40+
41+
```powershell
42+
Add-PnPFileSensitivityLabel -Identity "/sites/Marketing/Shared Documents/Report.pptx" -SensitivityLabelId "" -JustificationText "Previous label no longer applies" -AssignmentMethod Privileged
43+
```
44+
45+
## PARAMETERS
46+
47+
### -Identity
48+
The server relative path to the file, the unique identifier of the file, the listitem representing the file, or the file object itself on which we are adding the sensitivity label.
49+
50+
```yaml
51+
Type: FilePipeBind
52+
Parameter Sets: (All)
53+
54+
Required: True
55+
Position: 0
56+
Default value: None
57+
Accept pipeline input: True
58+
Accept wildcard characters: False
59+
```
60+
61+
### -SensitivityLabelId
62+
ID of the sensitivity label to be assigned, or empty string to remove the sensitivity label.
63+
64+
```yaml
65+
Type: string
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+
### -AssignmentMethod
76+
The assignment method of the label on the document. Indicates whether the assignment of the label was done automatically, standard, or as a privileged operation (the equivalent of an administrator operation).
77+
78+
```yaml
79+
Type: Guid
80+
Parameter Sets: (All)
81+
Accepted values: Standard, Privileged, Auto
82+
Required: False
83+
Position: Named
84+
Default value: None
85+
Accept pipeline input: True
86+
Accept wildcard characters: False
87+
```
88+
89+
### -JustificationText
90+
Justification text for audit purposes, and is required when downgrading/removing a label.
91+
92+
```yaml
93+
Type: Guid
94+
Parameter Sets: (All)
95+
96+
Required: False
97+
Position: Named
98+
Default value: None
99+
Accept pipeline input: True
100+
Accept wildcard characters: False
101+
```
102+
103+
## RELATED LINKS
104+
105+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace PnP.PowerShell.Commands.Enums
2+
{
3+
public enum SensitivityLabelAssignmentMethod
4+
{
5+
Standard,
6+
Privileged,
7+
Auto
8+
}
9+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using PnP.Core.Model.SharePoint;
2+
using PnP.PowerShell.Commands.Attributes;
3+
using PnP.PowerShell.Commands.Base;
4+
using PnP.PowerShell.Commands.Base.PipeBinds;
5+
using PnP.PowerShell.Commands.Utilities.REST;
6+
using System.Management.Automation;
7+
using System.Net.Http.Headers;
8+
9+
namespace PnP.PowerShell.Commands.Files
10+
{
11+
[Cmdlet(VerbsCommon.Add, "PnPFileSensitivityLabel")]
12+
[RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")]
13+
[RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")]
14+
15+
public class AddFileSensitivityLabel : PnPGraphCmdlet
16+
{
17+
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)]
18+
public FilePipeBind Identity;
19+
20+
[Parameter(Mandatory = true)]
21+
public string SensitivityLabelId;
22+
23+
[Parameter(Mandatory = false)]
24+
public Enums.SensitivityLabelAssignmentMethod AssignmentMethod = Enums.SensitivityLabelAssignmentMethod.Privileged;
25+
26+
[Parameter(Mandatory = false)]
27+
public string JustificationText = string.Empty;
28+
29+
protected override void ExecuteCmdlet()
30+
{
31+
var serverRelativeUrl = string.Empty;
32+
33+
IFile file = Identity.GetCoreFile(Connection.PnPContext, this);
34+
file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID);
35+
36+
var requestUrl = $"https://{Connection.GraphEndPoint}/v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/assignSensitivityLabel";
37+
38+
var payload = new
39+
{
40+
sensitivityLabelId = SensitivityLabelId,
41+
assignmentMethod = AssignmentMethod.ToString(),
42+
justificationText = JustificationText
43+
};
44+
45+
HttpResponseHeaders responseHeader = RestHelper.PostGetResponseHeader<string>(Connection.HttpClient, requestUrl, AccessToken, payload: payload);
46+
47+
WriteVerbose($"File sensitivity label assigned to {file.Name}");
48+
WriteObject(responseHeader.Location);
49+
}
50+
}
51+
}

src/Commands/Utilities/REST/RestHelper.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.SharePoint.Client;
12
using System;
23
using System.Collections.Generic;
34
using System.Linq;
@@ -7,16 +8,15 @@
78
using System.Text.Json;
89
using System.Text.Json.Serialization;
910
using System.Threading;
10-
using Microsoft.SharePoint.Client;
1111

1212
namespace PnP.PowerShell.Commands.Utilities.REST
1313
{
1414
internal static class RestHelper
15-
{
15+
{
1616
#region GET
1717
public static T ExecuteGetRequest<T>(ClientContext context, string url, string select = null, string filter = null, string expand = null, uint? top = null)
1818
{
19-
var returnValue = ExecuteGetRequest(context, url, select, filter, expand, top);
19+
var returnValue = ExecuteGetRequest(context, url, select, filter, expand, top);
2020

2121
var returnObject = JsonSerializer.Deserialize<T>(returnValue, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
2222
return returnObject;
@@ -208,9 +208,9 @@ public static T Get<T>(HttpClient httpClient, string url, ClientContext clientCo
208208
return default(T);
209209
}
210210

211-
#endregion
211+
#endregion
212212

213-
#region POST
213+
#region POST
214214

215215
public static string Post(HttpClient httpClient, string url, string accessToken, string accept = "application/json")
216216
{
@@ -270,7 +270,7 @@ public static string Post(HttpClient httpClient, string url, ClientContext clien
270270
message = GetMessage(url, HttpMethod.Post, clientContext, accept);
271271
}
272272
return SendMessage(httpClient, message);
273-
}
273+
}
274274

275275
public static T Post<T>(HttpClient httpClient, string url, string accessToken, object payload, bool camlCasePolicy = true)
276276
{
@@ -333,9 +333,9 @@ public static T Post<T>(HttpClient httpClient, string url, ClientContext clientC
333333
}
334334

335335

336-
#endregion
336+
#endregion
337337

338-
#region PATCH
338+
#region PATCH
339339
public static T Patch<T>(HttpClient httpClient, string url, string accessToken, object payload, bool camlCasePolicy = true)
340340
{
341341
var stringContent = Patch(httpClient, url, accessToken, payload);
@@ -360,7 +360,7 @@ public static T Patch<T>(HttpClient httpClient, string url, string accessToken,
360360

361361
public static string Patch(HttpClient httpClient, string url, string accessToken, object payload, string accept = "application/json")
362362
{
363-
HttpRequestMessage message = null;
363+
HttpRequestMessage message = null;
364364
if (payload != null)
365365
{
366366
var content = new StringContent(JsonSerializer.Serialize(payload, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }));
@@ -373,9 +373,9 @@ public static string Patch(HttpClient httpClient, string url, string accessToken
373373
}
374374
return SendMessage(httpClient, message);
375375
}
376-
#endregion
376+
#endregion
377377

378-
#region PUT
378+
#region PUT
379379
public static T ExecutePutRequest<T>(ClientContext context, string url, string content, string select = null, string filter = null, string expand = null, string contentType = null)
380380
{
381381
HttpContent stringContent = new StringContent(content);
@@ -439,9 +439,9 @@ private static HttpResponseMessage ExecutePutRequestInternal(ClientContext conte
439439
var returnValue = client.PutAsync(url, content).GetAwaiter().GetResult();
440440
return returnValue;
441441
}
442-
#endregion
442+
#endregion
443443

444-
#region MERGE
444+
#region MERGE
445445
public static T ExecuteMergeRequest<T>(ClientContext context, string url, string content, string select = null, string filter = null, string expand = null, string contentType = null)
446446
{
447447
HttpContent stringContent = new StringContent(content);
@@ -506,9 +506,9 @@ private static HttpResponseMessage ExecuteMergeRequestInternal(ClientContext con
506506
var returnValue = client.PostAsync(url, content).GetAwaiter().GetResult();
507507
return returnValue;
508508
}
509-
#endregion
509+
#endregion
510510

511-
#region DELETE
511+
#region DELETE
512512

513513
public static string Delete(HttpClient httpClient, string url, string accessToken, string accept = "application/json")
514514
{
@@ -584,7 +584,7 @@ private static HttpResponseMessage ExecuteDeleteRequestInternal(ClientContext co
584584
var returnValue = client.DeleteAsync(url).GetAwaiter().GetResult();
585585
return returnValue;
586586
}
587-
#endregion
587+
#endregion
588588

589589
private static HttpRequestMessage GetMessage(string url, HttpMethod method, string accessToken, string accept = "application/json", HttpContent content = null)
590590
{
@@ -693,7 +693,7 @@ private static System.Net.Http.Headers.HttpResponseHeaders SendMessageGetRespons
693693
}
694694
else
695695
{
696-
var errorContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); ;
696+
var errorContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
697697
throw new HttpRequestException(errorContent);
698698
}
699699
}

0 commit comments

Comments
 (0)