Skip to content

Commit 37cb2fa

Browse files
authored
Add support for batch processing in Add-PnPFileSensitivityLabel cmdlet (#5095)
* Add support for batch processing in Add-PnPFileSensitivityLabel cmdlet * Refactor AssignLabelImmediately and QueueBatchRequest methods for improved clarity and efficiency
1 parent 06f1c9a commit 37cb2fa

File tree

2 files changed

+98
-17
lines changed

2 files changed

+98
-17
lines changed

documentation/Add-PnPFileSensitivityLabel.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ Add the sensitivity label information for a file in SharePoint.
1919

2020
## SYNTAX
2121
```powershell
22-
Add-PnPFileSensitivityLabel -Identity <String> -SensitivityLabelId <Guid> -AssignmentMethod <Enum> -JustificationText <string>
22+
Add-PnPFileSensitivityLabel -Identity <String> -SensitivityLabelId <Guid> [-AssignmentMethod <Enum>] [-JustificationText <string>] [-Batch <PnPBatch>]
2323
```
2424

2525
## DESCRIPTION
2626

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.
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. You can optionally provide a PnP batch so the label assignment is queued and executed together with other batched Graph calls.
2828

2929
## EXAMPLES
3030

@@ -42,6 +42,19 @@ This example removes the sensitivity label information for the file at the speci
4242
Add-PnPFileSensitivityLabel -Identity "/sites/Marketing/Shared Documents/Report.pptx" -SensitivityLabelId "" -JustificationText "Previous label no longer applies" -AssignmentMethod Privileged
4343
```
4444

45+
### Example 3
46+
This example queues two label assignments in a single Microsoft Graph batch for improved throughput.
47+
48+
```powershell
49+
$batch = New-PnPBatch
50+
51+
Get-PnPFile -Folder "/sites/Marketing/Shared Documents" -Recursive -Filter "*.pptx" | ForEach-Object {
52+
Add-PnPFileSensitivityLabel -Identity $_ -SensitivityLabelId "b5b11b04-05b3-4fe4-baa9-b7f5f65b8b64" -AssignmentMethod Privileged -Batch $batch
53+
}
54+
55+
Invoke-PnPBatch -Batch $batch
56+
```
57+
4558
## PARAMETERS
4659

4760
### -Identity
@@ -100,6 +113,20 @@ Accept pipeline input: True
100113
Accept wildcard characters: False
101114
```
102115
116+
### -Batch
117+
Allows queueing the label assignment in an existing PnP batch. Use `Invoke-PnPBatch` to execute all queued operations.
118+
119+
```yaml
120+
Type: PnPBatch
121+
Parameter Sets: Batch
122+
123+
Required: True
124+
Position: Named
125+
Default value: None
126+
Accept pipeline input: False
127+
Accept wildcard characters: False
128+
```
129+
103130
## RELATED LINKS
104131

105132
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,107 @@
1-
using PnP.Core.Model.SharePoint;
1+
using PnP.Core.Model;
2+
using PnP.Core.Model.SharePoint;
3+
using PnP.Core.Services;
24
using PnP.PowerShell.Commands.Attributes;
35
using PnP.PowerShell.Commands.Base;
46
using PnP.PowerShell.Commands.Base.PipeBinds;
5-
using PnP.PowerShell.Commands.Utilities.REST;
7+
using PnP.PowerShell.Commands.Model;
8+
using System.Collections.Generic;
69
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;
814

915
namespace PnP.PowerShell.Commands.Files
1016
{
11-
[Cmdlet(VerbsCommon.Add, "PnPFileSensitivityLabel")]
17+
[Cmdlet(VerbsCommon.Add, "PnPFileSensitivityLabel", DefaultParameterSetName = ParameterSet_SINGLE)]
1218
[RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")]
1319
[RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")]
1420

1521
public class AddFileSensitivityLabel : PnPGraphCmdlet
1622
{
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)]
1828
public FilePipeBind Identity;
1929

20-
[Parameter(Mandatory = true)]
30+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_SINGLE)]
31+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BATCH)]
2132
[AllowNull]
2233
[AllowEmptyString]
2334
public string SensitivityLabelId;
2435

25-
[Parameter(Mandatory = false)]
36+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SINGLE)]
37+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BATCH)]
2638
public Enums.SensitivityLabelAssignmentMethod AssignmentMethod = Enums.SensitivityLabelAssignmentMethod.Privileged;
2739

28-
[Parameter(Mandatory = false)]
40+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SINGLE)]
41+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BATCH)]
2942
public string JustificationText = string.Empty;
3043

44+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BATCH)]
45+
public PnPBatch Batch;
46+
3147
protected override void ExecuteCmdlet()
3248
{
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));
3481

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+
}
3784

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+
}
3989

90+
private string SerializePayload()
91+
{
4092
var payload = new
4193
{
4294
sensitivityLabelId = SensitivityLabelId,
4395
assignmentMethod = AssignmentMethod.ToString(),
4496
justificationText = JustificationText
4597
};
4698

47-
HttpResponseHeaders responseHeader = RestHelper.PostGetResponseHeader<string>(Connection.HttpClient, requestUrl, AccessToken, payload: payload);
99+
var serializerOptions = new JsonSerializerOptions
100+
{
101+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
102+
};
48103

49-
LogDebug($"File sensitivity label assigned to {file.Name}");
50-
WriteObject(responseHeader.Location);
104+
return JsonSerializer.Serialize(payload, serializerOptions);
51105
}
52106
}
53107
}

0 commit comments

Comments
 (0)