From 82f7cbc48329c7024c5f7e3475639b5e9b6548b8 Mon Sep 17 00:00:00 2001 From: reshmee011 Date: Tue, 3 Dec 2024 06:46:20 +0000 Subject: [PATCH 1/2] New cmdlet --- documentation/Get-PnPFileRetentionLabel.md | 58 +++++++++++++++++ src/Commands/Files/GetFileRetentionLabel.cs | 57 +++++++++++++++++ .../Model/Graph/Purview/FileRetentionLabel.cs | 64 +++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 documentation/Get-PnPFileRetentionLabel.md create mode 100644 src/Commands/Files/GetFileRetentionLabel.cs create mode 100644 src/Commands/Model/Graph/Purview/FileRetentionLabel.cs diff --git a/documentation/Get-PnPFileRetentionLabel.md b/documentation/Get-PnPFileRetentionLabel.md new file mode 100644 index 000000000..c4fbcef6a --- /dev/null +++ b/documentation/Get-PnPFileRetentionLabel.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-PnPFileRetentionLabel.html +external help file: PnP.PowerShell.dll-Help.xml +title: Get-PnPFileRetentionLabel +--- + +# Get-PnPFileRetentionLabel + +## SYNOPSIS + +**Required Permissions** + + * Microsoft Graph API : One of Files.Read.All, Sites.Read.All, Files.ReadWrite.All, Sites.ReadWrite.All + +Retrieves the retention label information for a file in SharePoint. + +## SYNTAX +```powershell +Get-PnPFileRetentionLabel -Url +``` + +## DESCRIPTION + +The Get-PnPFileRetentionLabel cmdlet retrieves the retention 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 retention label information for the file at the specified URL. + +```powershell +Get-PnPFileRetentionLabel -Url "/sites/Marketing/Shared Documents/Report.pptx" +``` + +This example retrieves the retention label information for the file at the specified URL. + +## PARAMETERS + +### -Url +Specifies the URL of the file for which to retrieve the retention 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/GetFileRetentionLabel.cs b/src/Commands/Files/GetFileRetentionLabel.cs new file mode 100644 index 000000000..0d1b892b4 --- /dev/null +++ b/src/Commands/Files/GetFileRetentionLabel.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, "PnPFileRetentionLabel")] + [RequiredApiDelegatedOrApplicationPermissions("graph/Files.Read.All")] + [RequiredApiDelegatedOrApplicationPermissions("graph/Sites.Read.All")] + [RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")] + [RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")] + [OutputType(typeof(FileRetentionLabel))] + public class GetFileRetentionLabel : 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}/retentionLabel"; + + var results = GraphHelper.Get(this, Connection, requestUrl, AccessToken); + WriteObject(results, true); + } + } +} diff --git a/src/Commands/Model/Graph/Purview/FileRetentionLabel.cs b/src/Commands/Model/Graph/Purview/FileRetentionLabel.cs new file mode 100644 index 000000000..1394cafcd --- /dev/null +++ b/src/Commands/Model/Graph/Purview/FileRetentionLabel.cs @@ -0,0 +1,64 @@ +using System; +using System.Text.Json.Serialization; + +namespace PnP.PowerShell.Commands.Model.Graph.Purview +{ + using System; + using System.Text.Json.Serialization; + + public class FileRetentionLabel + { + [JsonPropertyName("name")] + public string Name { get; set; } + + [JsonPropertyName("retentionSettings")] + public RetentionSettings RetentionSettings { get; set; } + + [JsonPropertyName("isLabelAppliedExplicitly")] + public bool IsLabelAppliedExplicitly { get; set; } + + [JsonPropertyName("labelAppliedDateTime")] + public DateTime LabelAppliedDateTime { get; set; } + + [JsonPropertyName("labelAppliedBy")] + public LabelAppliedBy LabelAppliedBy { get; set; } + } + + public class RetentionSettings + { + [JsonPropertyName("behaviorDuringRetentionPeriod")] + public string BehaviorDuringRetentionPeriod { get; set; } + + [JsonPropertyName("isDeleteAllowed")] + public bool IsDeleteAllowed { get; set; } + + [JsonPropertyName("isRecordLocked")] + public bool IsRecordLocked { get; set; } + + [JsonPropertyName("isMetadataUpdateAllowed")] + public bool IsMetadataUpdateAllowed { get; set; } + + [JsonPropertyName("isContentUpdateAllowed")] + public bool IsContentUpdateAllowed { get; set; } + + [JsonPropertyName("isLabelUpdateAllowed")] + public bool IsLabelUpdateAllowed { get; set; } + } + + public class LabelAppliedBy + { + [JsonPropertyName("user")] + public User User { get; set; } + } + + public class User + { + [JsonPropertyName("id")] + public string Id { get; set; } + + [JsonPropertyName("displayName")] + public string DisplayName { get; set; } + } + +} + From 96282bd05c5ceee7c647cf3172cd74d6ea9f8529 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sun, 15 Dec 2024 15:43:20 +0200 Subject: [PATCH 2/2] Fix build issue --- src/Commands/Files/GetFileRetentionLabel.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/Files/GetFileRetentionLabel.cs b/src/Commands/Files/GetFileRetentionLabel.cs index 0d1b892b4..bab597501 100644 --- a/src/Commands/Files/GetFileRetentionLabel.cs +++ b/src/Commands/Files/GetFileRetentionLabel.cs @@ -50,7 +50,7 @@ protected override void ExecuteCmdlet() var requestUrl = $"v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/retentionLabel"; - var results = GraphHelper.Get(this, Connection, requestUrl, AccessToken); + var results = RequestHelper.Get(requestUrl); WriteObject(results, true); } }