From a576c0b7501f214d3f64a450dd98044793f21c4f Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sun, 10 Nov 2024 21:50:59 +0200 Subject: [PATCH] Add Get-PnPFileSensitivityLabel cmdlet for retrieving file sensitivity labels in SharePoint --- documentation/Get-PnPFileSensitivityLabel.md | 58 +++++++++++++++++++ src/Commands/Files/GetFileSensitivityLabel.cs | 57 ++++++++++++++++++ .../Model/Graph/Purview/SensitivityLabel.cs | 26 +++++++++ 3 files changed, 141 insertions(+) create mode 100644 documentation/Get-PnPFileSensitivityLabel.md create mode 100644 src/Commands/Files/GetFileSensitivityLabel.cs create mode 100644 src/Commands/Model/Graph/Purview/SensitivityLabel.cs diff --git a/documentation/Get-PnPFileSensitivityLabel.md b/documentation/Get-PnPFileSensitivityLabel.md new file mode 100644 index 000000000..4738551de --- /dev/null +++ b/documentation/Get-PnPFileSensitivityLabel.md @@ -0,0 +1,58 @@ +--- +Module Name: PnP.PowerShell +schema: 2.0.0 +applicable: SharePoint Online +online version: https://pnp.github.io/powershell/cmdlets/Get-PnPFileSensitivityLabel.html +external help file: PnP.PowerShell.dll-Help.xml +title: Get-PnPFileSensitivityLabel +--- + +# Get-PnPFileSensitivityLabel + +## SYNOPSIS + +**Required Permissions** + + * Microsoft Graph API : One of Files.Read.All, Sites.Read.All, Files.ReadWrite.All, Sites.ReadWrite.All + +Retrieves the sensitivity label information for a file in SharePoint. + +## SYNTAX +```powershell +Get-PnPFileSensitivityLabel -Url +``` + +## DESCRIPTION + +The Get-PnPFileSensitivityLabel cmdlet retrieves 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. + +## EXAMPLES + +### Example 1 +This example retrieves the sensitivity label information for the file at the specified URL. + +```powershell +Get-PnPFileSensitivityLabel -Url "/sites/Marketing/Shared Documents/Report.pptx" +``` + +This example retrieves the sensitivity label information for the file at the specified URL. + +## PARAMETERS + +### -Url +Specifies the URL of the file for which to retrieve the sensitivity label information. + +```yaml +Type: String +Parameter Sets: (All) + +Required: True +Position: Named +Default value: None +Accept pipeline input: True +Accept wildcard characters: False +``` + +## RELATED LINKS + +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/src/Commands/Files/GetFileSensitivityLabel.cs b/src/Commands/Files/GetFileSensitivityLabel.cs new file mode 100644 index 000000000..371c494c3 --- /dev/null +++ b/src/Commands/Files/GetFileSensitivityLabel.cs @@ -0,0 +1,57 @@ +using PnP.Framework.Utilities; +using PnP.PowerShell.Commands.Attributes; +using PnP.PowerShell.Commands.Base; +using PnP.PowerShell.Commands.Model.Graph.Purview; +using PnP.PowerShell.Commands.Utilities.REST; +using System; +using System.Management.Automation; + +namespace PnP.PowerShell.Commands.Files +{ + [Cmdlet(VerbsCommon.Get, "PnPFileSensitivityLabel")] + [RequiredApiDelegatedOrApplicationPermissions("graph/Files.Read.All")] + [RequiredApiDelegatedOrApplicationPermissions("graph/Sites.Read.All")] + [RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")] + [RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")] + [OutputType(typeof(SensitivityLabels))] + public class GetFileSensitivityLabel : PnPGraphCmdlet + { + [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] + public string Url = string.Empty; + + protected override void ExecuteCmdlet() + { + var serverRelativeUrl = string.Empty; + + if (Uri.IsWellFormedUriString(Url, UriKind.Absolute)) + { + // We can't deal with absolute URLs + Url = UrlUtility.MakeRelativeUrl(Url); + } + + // 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. + Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B")); + + Connection.PnPContext.Web.EnsureProperties(w => w.ServerRelativeUrl); + + var webUrl = Connection.PnPContext.Web.ServerRelativeUrl; + + if (!Url.ToLower().StartsWith(webUrl.ToLower())) + { + serverRelativeUrl = UrlUtility.Combine(webUrl, Url); + } + else + { + serverRelativeUrl = Url; + } + + var file = Connection.PnPContext.Web.GetFileByServerRelativeUrl(Url); + file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID); + + var requestUrl = $"v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/extractSensitivityLabels"; + + var results = GraphHelper.Post(this, Connection, requestUrl, AccessToken); + WriteObject(results.Labels, true); + } + } +} diff --git a/src/Commands/Model/Graph/Purview/SensitivityLabel.cs b/src/Commands/Model/Graph/Purview/SensitivityLabel.cs new file mode 100644 index 000000000..2142562f8 --- /dev/null +++ b/src/Commands/Model/Graph/Purview/SensitivityLabel.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace PnP.PowerShell.Commands.Model.Graph.Purview +{ + public class SensitivityLabel + { + [JsonPropertyName("sensitivityLabelId")] + public string SensitivityLabelId { get; set; } + + [JsonPropertyName("assignmentMethod")] + public string AssignmentMethod { get; set; } + + [JsonPropertyName("tenantId")] + public string TenantId { get; set; } + } + + public class SensitivityLabels + { + [JsonPropertyName("@odata.type")] + public string Odatatype { get; set; } + + [JsonPropertyName("labels")] + public List Labels { get; set; } + } +}