|
1 | | -using PnP.Core.Model.SharePoint; |
| 1 | +using PnP.Core.Model; |
| 2 | +using PnP.Core.Model.SharePoint; |
| 3 | +using PnP.Core.Services; |
2 | 4 | using PnP.PowerShell.Commands.Attributes; |
3 | 5 | using PnP.PowerShell.Commands.Base; |
4 | 6 | using PnP.PowerShell.Commands.Base.PipeBinds; |
5 | | -using PnP.PowerShell.Commands.Utilities.REST; |
| 7 | +using PnP.PowerShell.Commands.Model; |
| 8 | +using System.Collections.Generic; |
6 | 9 | using System.Management.Automation; |
7 | | -using System.Net.Http.Headers; |
| 10 | +using System.Net.Http; |
| 11 | +using System.Text; |
| 12 | +using System.Text.Json; |
| 13 | +using System.Text.Json.Serialization; |
8 | 14 |
|
9 | 15 | namespace PnP.PowerShell.Commands.Files |
10 | 16 | { |
11 | | - [Cmdlet(VerbsCommon.Add, "PnPFileSensitivityLabel")] |
| 17 | + [Cmdlet(VerbsCommon.Add, "PnPFileSensitivityLabel", DefaultParameterSetName = ParameterSet_SINGLE)] |
12 | 18 | [RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")] |
13 | 19 | [RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")] |
14 | 20 |
|
15 | 21 | public class AddFileSensitivityLabel : PnPGraphCmdlet |
16 | 22 | { |
17 | | - [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)] |
| 23 | + private const string ParameterSet_SINGLE = "Single"; |
| 24 | + private const string ParameterSet_BATCH = "Batch"; |
| 25 | + |
| 26 | + [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = ParameterSet_SINGLE)] |
| 27 | + [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = ParameterSet_BATCH)] |
18 | 28 | public FilePipeBind Identity; |
19 | 29 |
|
20 | | - [Parameter(Mandatory = true)] |
| 30 | + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_SINGLE)] |
| 31 | + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BATCH)] |
21 | 32 | [AllowNull] |
22 | 33 | [AllowEmptyString] |
23 | 34 | public string SensitivityLabelId; |
24 | 35 |
|
25 | | - [Parameter(Mandatory = false)] |
| 36 | + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_SINGLE)] |
| 37 | + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_BATCH)] |
26 | 38 | public Enums.SensitivityLabelAssignmentMethod AssignmentMethod = Enums.SensitivityLabelAssignmentMethod.Privileged; |
27 | 39 |
|
28 | | - [Parameter(Mandatory = false)] |
| 40 | + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_SINGLE)] |
| 41 | + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_BATCH)] |
29 | 42 | public string JustificationText = string.Empty; |
30 | 43 |
|
| 44 | + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BATCH)] |
| 45 | + public PnPBatch Batch; |
| 46 | + |
31 | 47 | protected override void ExecuteCmdlet() |
32 | 48 | { |
33 | | - var serverRelativeUrl = string.Empty; |
| 49 | + var context = ParameterSpecified(nameof(Batch)) ? Batch.Context : Connection.PnPContext; |
| 50 | + |
| 51 | + IFile file = Identity.GetCoreFile(context, this); |
| 52 | + file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID, f => f.Name); |
| 53 | + |
| 54 | + var requestUrl = GetRequestUrl(file); |
| 55 | + var payloadJson = SerializePayload(); |
| 56 | + |
| 57 | + if (ParameterSpecified(nameof(Batch))) |
| 58 | + { |
| 59 | + QueueBatchRequest(requestUrl, payloadJson, file); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + AssignLabelImmediately(requestUrl, payloadJson, file); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void AssignLabelImmediately(string requestUrl, string payloadJson, IFile file) |
| 68 | + { |
| 69 | + using var content = new StringContent(payloadJson, Encoding.UTF8, "application/json"); |
| 70 | + using var response = GraphRequestHelper.PostHttpContent(requestUrl, content); |
| 71 | + |
| 72 | + LogDebug($"File sensitivity label assigned to {file.Name}"); |
| 73 | + WriteObject(response?.Headers?.Location); |
| 74 | + } |
| 75 | + |
| 76 | + private void QueueBatchRequest(string requestUrl, string payloadJson, IFile file) |
| 77 | + { |
| 78 | + Batch.Context.Web.ExecuteRequestBatch( |
| 79 | + Batch.Batch, |
| 80 | + new ApiRequest(HttpMethod.Post, ApiRequestType.Graph, requestUrl, payloadJson)); |
34 | 81 |
|
35 | | - IFile file = Identity.GetCoreFile(Connection.PnPContext, this); |
36 | | - file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID); |
| 82 | + LogDebug($"Queued file sensitivity label assignment for {file.Name}"); |
| 83 | + } |
37 | 84 |
|
38 | | - var requestUrl = $"https://{Connection.GraphEndPoint}/v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/assignSensitivityLabel"; |
| 85 | + private static string GetRequestUrl(IFile file) |
| 86 | + { |
| 87 | + return $"v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/assignSensitivityLabel"; |
| 88 | + } |
39 | 89 |
|
| 90 | + private string SerializePayload() |
| 91 | + { |
40 | 92 | var payload = new |
41 | 93 | { |
42 | 94 | sensitivityLabelId = SensitivityLabelId, |
43 | 95 | assignmentMethod = AssignmentMethod.ToString(), |
44 | 96 | justificationText = JustificationText |
45 | 97 | }; |
46 | 98 |
|
47 | | - HttpResponseHeaders responseHeader = RestHelper.PostGetResponseHeader<string>(Connection.HttpClient, requestUrl, AccessToken, payload: payload); |
| 99 | + var serializerOptions = new JsonSerializerOptions |
| 100 | + { |
| 101 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 102 | + }; |
48 | 103 |
|
49 | | - LogDebug($"File sensitivity label assigned to {file.Name}"); |
50 | | - WriteObject(responseHeader.Location); |
| 104 | + return JsonSerializer.Serialize(payload, serializerOptions); |
51 | 105 | } |
52 | 106 | } |
53 | 107 | } |
0 commit comments