Skip to content

Commit a4fb397

Browse files
gautamdshethGautam Sheth
andauthored
Add Get-PnPFileSensitivityLabel cmdlet for retrieving file sensitivity labels in SharePoint (#4537)
Co-authored-by: Gautam Sheth <gautam.sheth@staffbase.com>
1 parent 1c29fb6 commit a4fb397

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
schema: 2.0.0
4+
applicable: SharePoint Online
5+
online version: https://pnp.github.io/powershell/cmdlets/Get-PnPFileSensitivityLabel.html
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
title: Get-PnPFileSensitivityLabel
8+
---
9+
10+
# Get-PnPFileSensitivityLabel
11+
12+
## SYNOPSIS
13+
14+
**Required Permissions**
15+
16+
* Microsoft Graph API : One of Files.Read.All, Sites.Read.All, Files.ReadWrite.All, Sites.ReadWrite.All
17+
18+
Retrieves the sensitivity label information for a file in SharePoint.
19+
20+
## SYNTAX
21+
```powershell
22+
Get-PnPFileSensitivityLabel -Url <String>
23+
```
24+
25+
## DESCRIPTION
26+
27+
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.
28+
29+
## EXAMPLES
30+
31+
### Example 1
32+
This example retrieves the sensitivity label information for the file at the specified URL.
33+
34+
```powershell
35+
Get-PnPFileSensitivityLabel -Url "/sites/Marketing/Shared Documents/Report.pptx"
36+
```
37+
38+
This example retrieves the sensitivity label information for the file at the specified URL.
39+
40+
## PARAMETERS
41+
42+
### -Url
43+
Specifies the URL of the file for which to retrieve the sensitivity label information.
44+
45+
```yaml
46+
Type: String
47+
Parameter Sets: (All)
48+
49+
Required: True
50+
Position: Named
51+
Default value: None
52+
Accept pipeline input: True
53+
Accept wildcard characters: False
54+
```
55+
56+
## RELATED LINKS
57+
58+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using PnP.Framework.Utilities;
2+
using PnP.PowerShell.Commands.Attributes;
3+
using PnP.PowerShell.Commands.Base;
4+
using PnP.PowerShell.Commands.Model.Graph.Purview;
5+
using PnP.PowerShell.Commands.Utilities.REST;
6+
using System;
7+
using System.Management.Automation;
8+
9+
namespace PnP.PowerShell.Commands.Files
10+
{
11+
[Cmdlet(VerbsCommon.Get, "PnPFileSensitivityLabel")]
12+
[RequiredApiDelegatedOrApplicationPermissions("graph/Files.Read.All")]
13+
[RequiredApiDelegatedOrApplicationPermissions("graph/Sites.Read.All")]
14+
[RequiredApiDelegatedOrApplicationPermissions("graph/Files.ReadWrite.All")]
15+
[RequiredApiDelegatedOrApplicationPermissions("graph/Sites.ReadWrite.All")]
16+
[OutputType(typeof(SensitivityLabels))]
17+
public class GetFileSensitivityLabel : PnPGraphCmdlet
18+
{
19+
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
20+
public string Url = string.Empty;
21+
22+
protected override void ExecuteCmdlet()
23+
{
24+
var serverRelativeUrl = string.Empty;
25+
26+
if (Uri.IsWellFormedUriString(Url, UriKind.Absolute))
27+
{
28+
// We can't deal with absolute URLs
29+
Url = UrlUtility.MakeRelativeUrl(Url);
30+
}
31+
32+
// 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.
33+
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));
34+
35+
Connection.PnPContext.Web.EnsureProperties(w => w.ServerRelativeUrl);
36+
37+
var webUrl = Connection.PnPContext.Web.ServerRelativeUrl;
38+
39+
if (!Url.ToLower().StartsWith(webUrl.ToLower()))
40+
{
41+
serverRelativeUrl = UrlUtility.Combine(webUrl, Url);
42+
}
43+
else
44+
{
45+
serverRelativeUrl = Url;
46+
}
47+
48+
var file = Connection.PnPContext.Web.GetFileByServerRelativeUrl(Url);
49+
file.EnsureProperties(f => f.VroomDriveID, f => f.VroomItemID);
50+
51+
var requestUrl = $"v1.0/drives/{file.VroomDriveID}/items/{file.VroomItemID}/extractSensitivityLabels";
52+
53+
var results = GraphHelper.Post<SensitivityLabels>(this, Connection, requestUrl, AccessToken);
54+
WriteObject(results.Labels, true);
55+
}
56+
}
57+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
4+
namespace PnP.PowerShell.Commands.Model.Graph.Purview
5+
{
6+
public class SensitivityLabel
7+
{
8+
[JsonPropertyName("sensitivityLabelId")]
9+
public string SensitivityLabelId { get; set; }
10+
11+
[JsonPropertyName("assignmentMethod")]
12+
public string AssignmentMethod { get; set; }
13+
14+
[JsonPropertyName("tenantId")]
15+
public string TenantId { get; set; }
16+
}
17+
18+
public class SensitivityLabels
19+
{
20+
[JsonPropertyName("@odata.type")]
21+
public string Odatatype { get; set; }
22+
23+
[JsonPropertyName("labels")]
24+
public List<SensitivityLabel> Labels { get; set; }
25+
}
26+
}

0 commit comments

Comments
 (0)