Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions documentation/Add-PnPFileSensitivityLabel.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ Add the sensitivity label information for a file in SharePoint.

## SYNTAX
```powershell
Add-PnPFileSensitivityLabel -Identity <String> -SensitivityLabelId <Guid> -AssignmentMethod <Enum> -JustificationText <string>
Add-PnPFileSensitivityLabel -Identity <String> -SensitivityLabelId <Guid> [-AssignmentMethod <Enum>] [-JustificationText <string>] [-Batch <PnPBatch>]
```

## DESCRIPTION

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.
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.

## EXAMPLES

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

### Example 3
This example queues two label assignments in a single Microsoft Graph batch for improved throughput.

```powershell
$batch = New-PnPBatch

Get-PnPFile -Folder "/sites/Marketing/Shared Documents" -Recursive -Filter "*.pptx" | ForEach-Object {
Add-PnPFileSensitivityLabel -Identity $_ -SensitivityLabelId "b5b11b04-05b3-4fe4-baa9-b7f5f65b8b64" -AssignmentMethod Privileged -Batch $batch
}

Invoke-PnPBatch -Batch $batch
```

## PARAMETERS

### -Identity
Expand Down Expand Up @@ -100,6 +113,20 @@ Accept pipeline input: True
Accept wildcard characters: False
```

### -Batch
Allows queueing the label assignment in an existing PnP batch. Use `Invoke-PnPBatch` to execute all queued operations.

```yaml
Type: PnPBatch
Parameter Sets: Batch

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Expand Down
89 changes: 74 additions & 15 deletions src/Commands/Files/AddFileSensitivityLabel.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,112 @@
using PnP.Core.Model.SharePoint;
using PnP.Core.Model;
using PnP.Core.Model.SharePoint;
using PnP.Core.Services;
using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Utilities.REST;
using PnP.PowerShell.Commands.Model;
using System.Collections.Generic;
using System.Management.Automation;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace PnP.PowerShell.Commands.Files
{
[Cmdlet(VerbsCommon.Add, "PnPFileSensitivityLabel")]
[Cmdlet(VerbsCommon.Add, "PnPFileSensitivityLabel", DefaultParameterSetName = ParameterSet_SINGLE)]
[RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")]
[RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")]

public class AddFileSensitivityLabel : PnPGraphCmdlet
{
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)]
private const string ParameterSet_SINGLE = "Single";
private const string ParameterSet_BATCH = "Batch";

[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = ParameterSet_SINGLE)]
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = ParameterSet_BATCH)]
public FilePipeBind Identity;

[Parameter(Mandatory = true)]
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_SINGLE)]
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BATCH)]
[AllowNull]
[AllowEmptyString]
public string SensitivityLabelId;

[Parameter(Mandatory = false)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SINGLE)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BATCH)]
public Enums.SensitivityLabelAssignmentMethod AssignmentMethod = Enums.SensitivityLabelAssignmentMethod.Privileged;

[Parameter(Mandatory = false)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SINGLE)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BATCH)]
public string JustificationText = string.Empty;

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BATCH)]
public PnPBatch Batch;

protected override void ExecuteCmdlet()
{
var serverRelativeUrl = string.Empty;
var context = ParameterSpecified(nameof(Batch)) ? Batch.Context : Connection.PnPContext;

IFile file = Identity.GetCoreFile(Connection.PnPContext, this);
file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID);
IFile file = Identity.GetCoreFile(context, this);
file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID, f => f.Name);

var requestUrl = $"https://{Connection.GraphEndPoint}/v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/assignSensitivityLabel";
var requestUrl = GetRequestUrl(file);
var payloadJson = SerializePayload();

if (ParameterSpecified(nameof(Batch)))
{
QueueBatchRequest(requestUrl, payloadJson, file);
}
else
{
AssignLabelImmediately(requestUrl, payloadJson, file);
}
}

private void AssignLabelImmediately(string requestUrl, string payloadJson, IFile file)
{
using var content = new StringContent(payloadJson, Encoding.UTF8, "application/json");
using var response = GraphRequestHelper.PostHttpContent(requestUrl, content);
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's trailing whitespace at the end of this line that should be removed for consistency.

Suggested change
using var response = GraphRequestHelper.PostHttpContent(requestUrl, content);
using var response = GraphRequestHelper.PostHttpContent(requestUrl, content);

Copilot uses AI. Check for mistakes.

LogDebug($"File sensitivity label assigned to {file.Name}");
WriteObject(response?.Headers?.Location);
}

private void QueueBatchRequest(string requestUrl, string payloadJson, IFile file)
{
Dictionary<string, string> headers = new()
{
{ "Content-Type", "application/json" }
};

Batch.Context.Web.WithHeaders(headers).ExecuteRequestBatch(
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The headers dictionary is created with only a Content-Type header, but this is typically handled automatically by the HTTP client when sending JSON content. Consider removing this manual header setting unless specifically required by the API.

Suggested change
Dictionary<string, string> headers = new()
{
{ "Content-Type", "application/json" }
};
Batch.Context.Web.WithHeaders(headers).ExecuteRequestBatch(
Batch.Context.Web.ExecuteRequestBatch(

Copilot uses AI. Check for mistakes.
Batch.Batch,
new ApiRequest(HttpMethod.Post, ApiRequestType.Graph, requestUrl, payloadJson));

LogDebug($"Queued file sensitivity label assignment for {file.Name}");
}

private static string GetRequestUrl(IFile file)
{
return $"v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/assignSensitivityLabel";
}

private string SerializePayload()
{
var payload = new
{
sensitivityLabelId = SensitivityLabelId,
assignmentMethod = AssignmentMethod.ToString(),
justificationText = JustificationText
};

HttpResponseHeaders responseHeader = RestHelper.PostGetResponseHeader<string>(Connection.HttpClient, requestUrl, AccessToken, payload: payload);
var serializerOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

LogDebug($"File sensitivity label assigned to {file.Name}");
WriteObject(responseHeader.Location);
return JsonSerializer.Serialize(payload, serializerOptions);
}
}
}
Loading