Skip to content

Commit fdad851

Browse files
authored
Merge pull request #2825 from KoenZomers/SetSensitivityLabelOnLibrary
Added `-DefaultSensitivityLabelForLibrary` to `Set-PnPList`
2 parents 891b0f6 + 229a418 commit fdad851

File tree

6 files changed

+153
-4
lines changed

6 files changed

+153
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
4040
- Added `-LargeList` parameter to `Remove-PnPList` cmdlet which improves the list recycling experience for Lists containing huge number of items. [#2778](https://github.com/pnp/powershell/pull/2778)
4141
- Added support for specifying the ContentUrl and WebsiteUrl configuration in `Add-PnPTeamsTab` cmdlet when trying to add a SharePoint page or list as a tab in Teams channel. [#2807](https://github.com/pnp/powershell/pull/2807)
4242
- Added `-CheckinType` parameter to `Add-PnPFile` cmdlet which provides the option to specify the checkin type for a file. The default value is set to `MinorCheckIn`. [#2806](https://github.com/pnp/powershell/pull/2806)
43+
- Added `-DefaultSensitivityLabelForLibrary` to `Set-PnPList` which allows setting the default sensitivity label for a library. [#2825](https://github.com/pnp/powershell/pull/2825)
4344
- Added `-ApplicationId` as alias for `-ClientId` in `Connect-PnPOnline` and `Request-PnPAccessToken` cmdlets. [#2805](https://github.com/pnp/powershell/pull/2805)
4445
- Added `-Connection` option to `Connect-PnPOnline` which allows of reusing an authenticated connection to connect to a different site [#2821](https://github.com/pnp/powershell/pull/2821)
4546
- Added `-UserAssignedManagedIdentityAzureResourceId` and `-UserAssignedManagedIdentityClientId` as alternatives to `-UserAssignedManagedIdentityObjectId` for `Connect-PnPOnline -ManagedIdentity` to provide an user managed identity to authenticate with. [#2813](https://github.com/pnp/powershell/pull/2813)

documentation/Set-PnPList.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Set-PnPList -Identity <ListPipeBind> [-EnableContentTypes <Boolean>] [-BreakRole
2121
[-EnableAttachments <Boolean>] [-EnableFolderCreation <Boolean>] [-EnableVersioning <Boolean>]
2222
[-EnableMinorVersions <Boolean>] [-MajorVersions <UInt32>] [-MinorVersions <UInt32>]
2323
[-EnableModeration <Boolean>] [-DraftVersionVisibility <DraftVisibilityType>] [-ReadSecurity <ListReadSecurity>] [-WriteSecurity <ListWriteSecurity>]
24-
[-NoCrawl] [-ExemptFromBlockDownloadOfNonViewableFiles <Boolean>] [-DisableGridEditing <Boolean>]
24+
[-NoCrawl] [-ExemptFromBlockDownloadOfNonViewableFiles <Boolean>] [-DisableGridEditing <Boolean>] [-DefaultSensitivityLabelForLibrary <SensitivityLabelPipeBind>]
2525
[-Path <String>] [-Connection <PnPConnection>] [<CommonParameters>]
2626
```
2727

@@ -79,6 +79,13 @@ Set-PnPList -Identity "Demo List" -Title "Demo List 2" -Path "Lists/DemoList2"
7979

8080
Rename a list, including its' URL.
8181

82+
### EXAMPLE 8
83+
```powershell
84+
Set-PnPList -Identity "Demo List" -DefaultSensitivityLabelForLibrary "Confidential"
85+
```
86+
87+
Sets the default sensitivity label for a document library to Confidential.
88+
8289
## PARAMETERS
8390

8491
### -BreakRoleInheritance
@@ -165,6 +172,20 @@ Accept pipeline input: False
165172
Accept wildcard characters: False
166173
```
167174
175+
### -DefaultSensitivityLabelForLibrary
176+
The instance, Id or name of the sensitivity label to set as the default for the library. If $null is provided, the default label will be removed.
177+
178+
```yaml
179+
Type: SensitivityLabelPipeBind
180+
Parameter Sets: (All)
181+
182+
Required: False
183+
Position: Named
184+
Default value: None
185+
Accept pipeline input: False
186+
Accept wildcard characters: False
187+
```
188+
168189
### -EnableAttachments
169190
Enable or disable attachments. Set to $true to enable, $false to disable.
170191
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace PnP.PowerShell.Commands.Base.PipeBinds
5+
{
6+
public sealed class SensitivityLabelPipeBind
7+
{
8+
private readonly Guid? _labelId;
9+
private readonly string _labelName;
10+
private readonly Model.Graph.Purview.InformationProtectionLabel _label;
11+
12+
public SensitivityLabelPipeBind()
13+
{
14+
_labelId = null;
15+
_labelName = string.Empty;
16+
}
17+
18+
public SensitivityLabelPipeBind(string id)
19+
{
20+
if (string.IsNullOrEmpty(id))
21+
{
22+
throw new ArgumentException(nameof(id));
23+
}
24+
25+
if (Guid.TryParse(id, out Guid labelId))
26+
{
27+
_labelId = labelId;
28+
}
29+
else
30+
{
31+
_labelName = id;
32+
}
33+
34+
_label = null;
35+
}
36+
37+
public SensitivityLabelPipeBind(Guid id)
38+
{
39+
_labelId = id;
40+
_labelName = string.Empty;
41+
_label = null;
42+
}
43+
44+
public SensitivityLabelPipeBind(Model.Graph.Purview.InformationProtectionLabel label)
45+
{
46+
_labelId = label.Id;
47+
_labelName = label.Name;
48+
_label = label;
49+
}
50+
51+
public string LabelName => _labelName;
52+
53+
public Guid? LabelId => _labelId;
54+
55+
public Model.Graph.Purview.InformationProtectionLabel Label => _label;
56+
57+
/// <summary>
58+
/// Tries to look up the Label by the Label Name
59+
/// </summary>
60+
/// <param name="connection">Connection that can be used to query Microsoft Graph for the available sensitivity labels</param>
61+
/// <param name="accesstoken">Access Token to use to authenticate to Microsoft Graph</param>
62+
/// <returns>The the sensitivity label that matches the name set in this pipebind or NULL if no match found</returns>
63+
public Model.Graph.Purview.InformationProtectionLabel GetLabelByNameThroughGraph(PnPConnection connection, string accesstoken)
64+
{
65+
if (string.IsNullOrEmpty(_labelName)) return null;
66+
67+
string url;
68+
if (connection.ConnectionMethod == Model.ConnectionMethod.AzureADAppOnly)
69+
{
70+
url = "/beta/security/informationProtection/sensitivityLabels";
71+
}
72+
else
73+
{
74+
url = "/beta/me/security/informationProtection/sensitivityLabels";
75+
}
76+
77+
var availableLabels = Utilities.REST.GraphHelper.GetResultCollectionAsync<Model.Graph.Purview.InformationProtectionLabel>(connection, $"https://{connection.GraphEndPoint}/{url}", accesstoken).GetAwaiter().GetResult();
78+
return availableLabels.FirstOrDefault(l => l.Name == _labelName);
79+
}
80+
}
81+
}

src/Commands/Lists/SetList.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using PnP.PowerShell.Commands.Base.PipeBinds;
44
using PnP.PowerShell.Commands.Enums;
5-
5+
using System;
66
using System.Management.Automation;
77

88
namespace PnP.PowerShell.Commands.Lists
@@ -86,6 +86,9 @@ public class SetList : PnPWebCmdlet
8686
[Parameter(Mandatory = false)]
8787
public string Path;
8888

89+
[Parameter(Mandatory = false)]
90+
public SensitivityLabelPipeBind DefaultSensitivityLabelForLibrary;
91+
8992
protected override void ExecuteCmdlet()
9093
{
9194
var list = Identity.GetList(CurrentWeb);
@@ -262,6 +265,48 @@ protected override void ExecuteCmdlet()
262265
}
263266
}
264267

268+
if(ParameterSpecified(nameof(DefaultSensitivityLabelForLibrary)))
269+
{
270+
if(DefaultSensitivityLabelForLibrary == null)
271+
{
272+
WriteVerbose("Removing sensitivity label from library");
273+
list.DefaultSensitivityLabelForLibrary = null;
274+
updateRequired = true;
275+
}
276+
else
277+
{
278+
if (DefaultSensitivityLabelForLibrary.LabelId.HasValue)
279+
{
280+
WriteVerbose($"Setting provided sensitivity label id '{DefaultSensitivityLabelForLibrary.LabelId}' as the default sensitivity label for the library");
281+
list.DefaultSensitivityLabelForLibrary = DefaultSensitivityLabelForLibrary.LabelId.ToString();
282+
updateRequired = true;
283+
}
284+
else
285+
{
286+
if (!string.IsNullOrEmpty(DefaultSensitivityLabelForLibrary.LabelName))
287+
{
288+
WriteVerbose($"Looking up sensitivity label id by label name '{DefaultSensitivityLabelForLibrary.LabelName}'");
289+
var label = DefaultSensitivityLabelForLibrary.GetLabelByNameThroughGraph(Connection, GraphAccessToken);
290+
291+
if (label == null || !label.Id.HasValue)
292+
{
293+
throw new ArgumentException($"Unable to find a sensitivity label with the provided name '{DefaultSensitivityLabelForLibrary.LabelName}'", nameof(DefaultSensitivityLabelForLibrary));
294+
}
295+
else
296+
{
297+
WriteVerbose($"Provided sensitivity label name '{DefaultSensitivityLabelForLibrary.LabelName}' resolved to sensitivity label id '{label.Id.Value}' and will be set as the default sensitivity label for the library");
298+
list.DefaultSensitivityLabelForLibrary = label.Id.Value.ToString();
299+
updateRequired = true;
300+
}
301+
}
302+
else
303+
{
304+
throw new ArgumentException($"Unable set the default sensitivity label for the library as there's no label name or label Id", nameof(DefaultSensitivityLabelForLibrary));
305+
}
306+
}
307+
}
308+
}
309+
265310
if (updateRequired)
266311
{
267312
list.Update();

src/Commands/Model/Graph/Purview/InformationProtectionLabel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Text.Json.Serialization;
23

34
namespace PnP.PowerShell.Commands.Model.Graph.Purview
@@ -12,7 +13,7 @@ public class InformationProtectionLabel
1213
/// The label ID is a globally unique identifier (GUID)
1314
/// </summary>
1415
[JsonPropertyName("id")]
15-
public string Id { get; set; }
16+
public Guid? Id { get; set; }
1617

1718
/// <summary>
1819
/// The plaintext name of the label.

src/Commands/Purview/SetSiteSensitivityLabel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected override void ExecuteCmdlet()
2929
throw new PSArgumentException($"No Microsoft Purview sensitivity label with the provided name '{Identity}' could be found", nameof(Identity));
3030
}
3131

32-
sensitivityLabelId = label.ElementAt(0).Id;
32+
sensitivityLabelId = label.ElementAt(0).Id?.ToString();
3333
WriteVerbose($"Microsoft Purview label with name '{Identity}' successfully resolved to Id '{sensitivityLabelId}'");
3434
}
3535
else

0 commit comments

Comments
 (0)