Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2766 from jensotto/set-field-allowdeletion
Browse files Browse the repository at this point in the history
Add support for setting AllowDeletion using Set-PnPField
  • Loading branch information
erwinvanhunen authored Jul 7, 2020
2 parents 03794f2 + 7f15152 commit 94e4381
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `-WithRightsAssignedDetailed` parameter to `Get-PnPUser` allowing for fine grained (broken) permissions on item, list and site level to be shown [PR #2754](https://github.com/pnp/PnP-PowerShell/pull/2754)
- Added a `-RowLimit` parameter to `Clear-PnPRecycleBinItem` and `Restore-PnPRecycleBinItem` so that it can be used on recycle bins which hold more than 5000 items [PR #2760](https://github.com/pnp/PnP-PowerShell/pull/2760)
- Added connection option to `Connect-PnPOnline` taking `-Scopes` and `-Credentials` to allow setting up a delegated permission token for use with Microsoft Graph and the Office 365 Management API. See [this wiki page](https://github.com/pnp/PnP-PowerShell/wiki/Connect-options#connect-using-scopes-and-credentials) for more details. [PR #2746](https://github.com/pnp/PnP-PowerShell/pull/2746)
- Added support for enabling and disabling fields using `Set-PnPField -Identity FieldName -Values @{AllowDeletion=$false}` [PR #2766](https://github.com/pnp/PnP-PowerShell/pull/2766)
- Added the following cmdlets to add/remove/clear owners and members of Microsoft 365 Groups: `Add-PnPMicrosoft365GroupMember`, `Add-PnPMicrosoft365GroupOwner`, `Remove-PnPMicrosoft365GroupMember`, `Remove-PnPMicrosoft365GroupOwner`, `Clear-PnPMicrosoft365GroupMember`, `Clear-PnPMicrosoft365GroupOwner` [PR #2750](https://github.com/pnp/PnP-PowerShell/pull/2750)
- Added Add-PnPTeamsChannel, Add-PnPTeamsTab, Add-PnPTeamsUser, Get-PnPTeamsApp, Get-PnPTeamsChannel, Get-PnPTeamsChannelMessage, Get-PnPTeamsTab, Get-PnPTeamsTeam, Get-PnPTeamsUser, New-PnPTeamsApp, New-PnPTeamsTeam, Remove-PnPTeamsChannel, Remove-PnPTeamsTab, Remove-PnPTeamsTeam, Remove-PnPTeamsUser, Set-PnPTeamsChannel, Set-PnPTeamsTab, Set-PnPTeamsTeam, Set-PnPTeamsPicture, Submit-PnPTeamsChannelMessage, Update-PnPTeamsApp cmdlets

Expand All @@ -28,6 +29,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Koen Zomers [koenzomers]
- Ellie Hussey [Professr]
- Todd Klindt [ToddKlindt]
- Jens Otto Hatlevold [jensotto]
- Robin Meure [robinmeure]
- Paul Bullock [pkbullock]

Expand Down
26 changes: 22 additions & 4 deletions Commands/Fields/SetField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SharePointPnP.PowerShell.Commands.Fields
[CmdletHelp("Changes one or more properties of a field in a specific list or for the whole web",
Category = CmdletHelpCategory.Fields,
OutputType = typeof(Field),
OutputTypeLink = "https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.field.aspx")]
OutputTypeLink = "https://docs.microsoft.com/previous-versions/office/sharepoint-server/ee545882(v=office.15)")]
[CmdletExample(
Code = @"PS:> Set-PnPField -Identity AssignedTo -Values @{JSLink=""customrendering.js"";Group=""My fields""}",
Remarks = @"Updates the AssignedTo field on the current web to use customrendering.js for the JSLink and sets the group name the field is categorized in to ""My Fields"". Lists that are already using the AssignedTo field will not be updated.",
Expand Down Expand Up @@ -40,6 +40,7 @@ public class SetField : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
const string allowDeletionPropertyKey = "AllowDeletion";
Field field = null;
if (List != null)
{
Expand Down Expand Up @@ -84,7 +85,14 @@ protected override void ExecuteCmdlet()
}
}

ClientContext.Load(field);
if (Values.ContainsKey(allowDeletionPropertyKey))
{
ClientContext.Load(field, f => f.SchemaXmlWithResourceTokens);
}
else
{
ClientContext.Load(field);
}
ClientContext.ExecuteQueryRetry();

// Get a reference to the type-specific object to allow setting type-specific properties, i.e. LookupList and LookupField for Microsoft.SharePoint.Client.FieldLookup
Expand All @@ -95,15 +103,25 @@ protected override void ExecuteCmdlet()
var value = Values[key];

var property = typeSpecificField.GetType().GetProperty(key);
if (property == null)

bool isAllowDeletionProperty = string.Equals(key, allowDeletionPropertyKey, StringComparison.Ordinal);

if (property == null && !isAllowDeletionProperty)
{
WriteWarning($"No property '{key}' found on this field. Value will be ignored.");
}
else
{
try
{
property.SetValue(typeSpecificField, value);
if (isAllowDeletionProperty)
{
field.SetAllowDeletion(value as bool?);
}
else
{
property.SetValue(typeSpecificField, value);
}
}
catch (Exception e)
{
Expand Down

0 comments on commit 94e4381

Please sign in to comment.