Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NullDereferenceException in Add-PnPField #2338

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Commands/Fields/AddField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,16 @@ protected override void ExecuteCmdlet()
}
if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
{
EnsureDynamicParameters(choiceFieldParameters);
f = list.CreateField<FieldChoice>(fieldCI);
((FieldChoice)f).Choices = choiceFieldParameters.Choices;
f.Update();
ClientContext.ExecuteQueryRetry();
}
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))
{
Expand Down Expand Up @@ -231,13 +234,15 @@ protected override void ExecuteCmdlet()

if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
{
EnsureDynamicParameters(choiceFieldParameters);
f = CurrentWeb.CreateField<FieldChoice>(fieldCI);
((FieldChoice)f).Choices = choiceFieldParameters.Choices;
f.Update();
ClientContext.ExecuteQueryRetry();
}
else if (Type == FieldType.Calculated)
{
EnsureDynamicParameters(calculatedFieldParameters);
f = CurrentWeb.CreateField<FieldCalculated>(fieldCI);
((FieldCalculated)f).Formula = calculatedFieldParameters.Formula;
f.Update();
Expand Down Expand Up @@ -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)]
Expand Down