-
Notifications
You must be signed in to change notification settings - Fork 396
Add support for batch processing in Add-PnPFileSensitivityLabel cmdlet #5095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||||||||||||||||
|
|
||||||||||||||||
| 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( | ||||||||||||||||
|
||||||||||||||||
| Dictionary<string, string> headers = new() | |
| { | |
| { "Content-Type", "application/json" } | |
| }; | |
| Batch.Context.Web.WithHeaders(headers).ExecuteRequestBatch( | |
| Batch.Context.Web.ExecuteRequestBatch( |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.