From cc32518145f34da28ca91ea90b4f2914076d34d5 Mon Sep 17 00:00:00 2001 From: Giacomo Pozzoni Date: Sun, 11 Sep 2022 16:14:17 +0200 Subject: [PATCH] Fix NullDereferenceException in Add-PnPField Fix NullDereferenceException in Add-PnPField when not including -Type parameter and specifying Choice, MultiChoice or Calculated as Type later on --- src/Commands/Fields/AddField.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Commands/Fields/AddField.cs b/src/Commands/Fields/AddField.cs index 0af47bcd1..32d2555fa 100644 --- a/src/Commands/Fields/AddField.cs +++ b/src/Commands/Fields/AddField.cs @@ -122,6 +122,7 @@ protected override void ExecuteCmdlet() } if (Type == FieldType.Choice || Type == FieldType.MultiChoice) { + EnsureDynamicParameters(choiceFieldParameters); f = list.CreateField(fieldCI); ((FieldChoice)f).Choices = choiceFieldParameters.Choices; f.Update(); @@ -129,6 +130,8 @@ protected override void ExecuteCmdlet() } else if (Type == FieldType.Calculated) { + EnsureDynamicParameters(calculatedFieldParameters); + // Either set the ResultType as input parameter or set it to the default Text if (!string.IsNullOrEmpty(calculatedFieldParameters.ResultType)) { @@ -231,6 +234,7 @@ protected override void ExecuteCmdlet() if (Type == FieldType.Choice || Type == FieldType.MultiChoice) { + EnsureDynamicParameters(choiceFieldParameters); f = CurrentWeb.CreateField(fieldCI); ((FieldChoice)f).Choices = choiceFieldParameters.Choices; f.Update(); @@ -238,6 +242,7 @@ protected override void ExecuteCmdlet() } else if (Type == FieldType.Calculated) { + EnsureDynamicParameters(calculatedFieldParameters); f = CurrentWeb.CreateField(fieldCI); ((FieldCalculated)f).Formula = calculatedFieldParameters.Formula; f.Update(); @@ -338,6 +343,14 @@ protected override void ExecuteCmdlet() } } + private void EnsureDynamicParameters(object dynamicParameters) + { + if (dynamicParameters == null) + { + throw new PSArgumentException($"Please specify the parameter -{nameof(Type)} when invoking this cmdlet", nameof(Type)); + } + } + public class ChoiceFieldDynamicParameters { [Parameter(Mandatory = false)]