Skip to content

Commit 534e6f7

Browse files
author
Gautam Sheth
committed
Add Add-PnPFileSensitivityLabel cmdlet for assigning sensitivity labels to SharePoint files
1 parent a4fb397 commit 534e6f7

File tree

4 files changed

+198
-17
lines changed

4 files changed

+198
-17
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 -Url <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 -Url "/sites/Marketing/Shared Documents/Report.pptx" -SensitivityLabelId b5b11b04-05b3-4fe4-baa9-b7f5f65b8b64 -JustificationText "Previous label no longer applies" -AssignmentMethod Privileged
36+
```
37+
38+
This example adds the sensitivity label information for the file at the specified URL.
39+
40+
## PARAMETERS
41+
42+
### -Url
43+
Specifies the URL of the file on which we add the sensitivity label information.
44+
45+
```yaml
46+
Type: String
47+
Parameter Sets: (All)
48+
49+
Required: True
50+
Position: Named
51+
Default value: None
52+
Accept pipeline input: True
53+
Accept wildcard characters: False
54+
```
55+
56+
### -SensitivityLabelId
57+
ID of the sensitivity label to be assigned, or empty string to remove the sensitivity label.
58+
59+
```yaml
60+
Type: string
61+
Parameter Sets: (All)
62+
63+
Required: True
64+
Position: Named
65+
Default value: None
66+
Accept pipeline input: True
67+
Accept wildcard characters: False
68+
```
69+
70+
### -AssignmentMethod
71+
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).
72+
73+
```yaml
74+
Type: Guid
75+
Parameter Sets: (All)
76+
Accepted values: Standard, Privileged, Auto
77+
Required: False
78+
Position: Named
79+
Default value: None
80+
Accept pipeline input: True
81+
Accept wildcard characters: False
82+
```
83+
84+
### -JustificationText
85+
Justification text for audit purposes, and is required when downgrading/removing a label.
86+
87+
```yaml
88+
Type: Guid
89+
Parameter Sets: (All)
90+
91+
Required: False
92+
Position: Named
93+
Default value: None
94+
Accept pipeline input: True
95+
Accept wildcard characters: False
96+
```
97+
98+
## RELATED LINKS
99+
100+
[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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using PnP.Framework.Utilities;
2+
using PnP.PowerShell.Commands.Attributes;
3+
using PnP.PowerShell.Commands.Base;
4+
using PnP.PowerShell.Commands.Utilities.REST;
5+
using System;
6+
using System.Management.Automation;
7+
8+
namespace PnP.PowerShell.Commands.Files
9+
{
10+
[Cmdlet(VerbsCommon.Add, "PnPFileSensitivityLabel")]
11+
[RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")]
12+
[RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")]
13+
14+
public class AddFileSensitivityLabel : PnPGraphCmdlet
15+
{
16+
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
17+
public string Url = string.Empty;
18+
19+
[Parameter(Mandatory = true)]
20+
public string SensitivityLabelId;
21+
22+
[Parameter(Mandatory = false)]
23+
public Enums.SensitivityLabelAssignmentMethod AssignmentMethod = Enums.SensitivityLabelAssignmentMethod.Privileged;
24+
25+
[Parameter(Mandatory = false)]
26+
public string JustificationText = string.Empty;
27+
28+
protected override void ExecuteCmdlet()
29+
{
30+
var serverRelativeUrl = string.Empty;
31+
32+
if (Uri.IsWellFormedUriString(Url, UriKind.Absolute))
33+
{
34+
// We can't deal with absolute URLs
35+
Url = UrlUtility.MakeRelativeUrl(Url);
36+
}
37+
38+
// Remove URL decoding from the Url as that will not work. We will encode the + character specifically, because if that is part of the filename, it needs to stay and not be decoded.
39+
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));
40+
41+
Connection.PnPContext.Web.EnsureProperties(w => w.ServerRelativeUrl);
42+
43+
var webUrl = Connection.PnPContext.Web.ServerRelativeUrl;
44+
45+
if (!Url.ToLower().StartsWith(webUrl.ToLower()))
46+
{
47+
serverRelativeUrl = UrlUtility.Combine(webUrl, Url);
48+
}
49+
else
50+
{
51+
serverRelativeUrl = Url;
52+
}
53+
54+
var file = Connection.PnPContext.Web.GetFileByServerRelativeUrl(Url);
55+
file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID);
56+
57+
var requestUrl = $"https://{Connection.GraphEndPoint}/v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/assignSensitivityLabel";
58+
59+
var payload = new
60+
{
61+
sensitivityLabelId = SensitivityLabelId,
62+
assignmentMethod = AssignmentMethod.ToString(),
63+
justificationText = JustificationText
64+
};
65+
66+
var responseHeader = RestHelper.PostGetResponseHeader<string>(Connection.HttpClient, requestUrl, AccessToken, payload: payload);
67+
68+
WriteVerbose($"File sensitivity label assigned to {Url}");
69+
WriteObject(responseHeader.Location);
70+
}
71+
}
72+
}

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)