Skip to content

Commit

Permalink
Fix #3503 : fix return type for field (#3510)
Browse files Browse the repository at this point in the history
Co-authored-by: Gautam Sheth <[email protected]>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Oct 22, 2023
1 parent 3e2b531 commit b33b5fe
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 21 deletions.
19 changes: 2 additions & 17 deletions documentation/Add-PnPField.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Add a field
```powershell
Add-PnPField [-List <ListPipeBind>] -DisplayName <String> -InternalName <String> -Type <FieldType>
[-Id <Guid>] [-Formula <String>] [-Choices <String>] [-AddToDefaultView] [-Required] [-Group <String>] [-ClientSideComponentId <Guid>]
[-ClientSideComponentProperties <String>] [-AddToAllContentTypes] [-ReturnType <String>] [-Connection <PnPConnection>]
[-ClientSideComponentProperties <String>] [-AddToAllContentTypes] [-Connection <PnPConnection>]
```

### Add field reference to list
Expand All @@ -30,7 +30,7 @@ Add-PnPField -List <ListPipeBind> -Field <FieldPipeBind> [-Connection <PnPConnec
### Add field to web
```powershell
Add-PnPField -DisplayName <String> -InternalName <String> -Type <FieldType> [-Id <Guid>] [-Formula <String>] [-Choices <String>]
[-ClientSideComponentId <Guid>] [-ClientSideComponentProperties <String>] [-ReturnType <String>]
[-ClientSideComponentId <Guid>] [-ClientSideComponentProperties <String>]
[-Connection <PnPConnection>]
```

Expand Down Expand Up @@ -304,21 +304,6 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -ReturnType
The return type of the calculated field. Only valid when Type Calculated is chosen.
```yaml
Type: FieldType
Parameter Sets: Add field to list, Add field to web
Accepted values: Integer, Text, DateTime, Boolean, Number, Currency

Required: False
Position: Named
Default value: Text
Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
168 changes: 164 additions & 4 deletions src/Commands/Fields/AddField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,87 @@ protected override void ExecuteCmdlet()
ClientContext.Load(f);
ClientContext.ExecuteQueryRetry();
}
WriteObject(f);
f.EnsureProperty(f => f.FieldTypeKind);
switch (f.FieldTypeKind)
{
case FieldType.DateTime:
{
WriteObject(ClientContext.CastTo<FieldDateTime>(f));
break;
}
case FieldType.Choice:
{
WriteObject(ClientContext.CastTo<FieldChoice>(f));
break;
}
case FieldType.Calculated:
{
var calculatedField = ClientContext.CastTo<FieldCalculated>(f);
calculatedField.EnsureProperty(fc => fc.Formula);
WriteObject(calculatedField);
break;
}
case FieldType.Computed:
{
WriteObject(ClientContext.CastTo<FieldComputed>(f));
break;
}
case FieldType.Geolocation:
{
WriteObject(ClientContext.CastTo<FieldGeolocation>(f));
break;

}
case FieldType.User:
{
WriteObject(ClientContext.CastTo<FieldUser>(f));
break;
}
case FieldType.Currency:
{
WriteObject(ClientContext.CastTo<FieldCurrency>(f));
break;
}
case FieldType.Guid:
{
WriteObject(ClientContext.CastTo<FieldGuid>(f));
break;
}
case FieldType.URL:
{
WriteObject(ClientContext.CastTo<FieldUrl>(f));
break;
}
case FieldType.Lookup:
{
WriteObject(ClientContext.CastTo<FieldLookup>(f));
break;
}
case FieldType.MultiChoice:
{
WriteObject(ClientContext.CastTo<FieldMultiChoice>(f));
break;
}
case FieldType.Number:
{
WriteObject(ClientContext.CastTo<FieldNumber>(f));
break;
}
case FieldType.Invalid:
{
if (f.TypeAsString.StartsWith("TaxonomyFieldType"))
{
WriteObject(ClientContext.CastTo<TaxonomyField>(f));
break;
}
goto default;
}
default:
{
WriteObject(f);
break;
}
}
}
else
{
Expand Down Expand Up @@ -207,7 +287,87 @@ protected override void ExecuteCmdlet()
list.Update();
ClientContext.ExecuteQueryRetry();
}
WriteObject(field);
field.EnsureProperty(f => f.FieldTypeKind);
switch (field.FieldTypeKind)
{
case FieldType.DateTime:
{
WriteObject(ClientContext.CastTo<FieldDateTime>(field));
break;
}
case FieldType.Choice:
{
WriteObject(ClientContext.CastTo<FieldChoice>(field));
break;
}
case FieldType.Calculated:
{
var calculatedField = ClientContext.CastTo<FieldCalculated>(field);
calculatedField.EnsureProperty(fc => fc.Formula);
WriteObject(calculatedField);
break;
}
case FieldType.Computed:
{
WriteObject(ClientContext.CastTo<FieldComputed>(field));
break;
}
case FieldType.Geolocation:
{
WriteObject(ClientContext.CastTo<FieldGeolocation>(field));
break;

}
case FieldType.User:
{
WriteObject(ClientContext.CastTo<FieldUser>(field));
break;
}
case FieldType.Currency:
{
WriteObject(ClientContext.CastTo<FieldCurrency>(field));
break;
}
case FieldType.Guid:
{
WriteObject(ClientContext.CastTo<FieldGuid>(field));
break;
}
case FieldType.URL:
{
WriteObject(ClientContext.CastTo<FieldUrl>(field));
break;
}
case FieldType.Lookup:
{
WriteObject(ClientContext.CastTo<FieldLookup>(field));
break;
}
case FieldType.MultiChoice:
{
WriteObject(ClientContext.CastTo<FieldMultiChoice>(field));
break;
}
case FieldType.Number:
{
WriteObject(ClientContext.CastTo<FieldNumber>(field));
break;
}
case FieldType.Invalid:
{
if (field.TypeAsString.StartsWith("TaxonomyFieldType"))
{
WriteObject(ClientContext.CastTo<TaxonomyField>(field));
break;
}
goto default;
}
default:
{
WriteObject(field);
break;
}
}
}
}
else
Expand Down Expand Up @@ -246,7 +406,7 @@ protected override void ExecuteCmdlet()
f = CurrentWeb.CreateField<FieldCalculated>(fieldCI);
((FieldCalculated)f).Formula = calculatedFieldParameters.Formula;

if(!string.IsNullOrEmpty(calculatedFieldParameters.ResultType) && Enum.TryParse<FieldType>(calculatedFieldParameters.ResultType, out FieldType resultType))
if (!string.IsNullOrEmpty(calculatedFieldParameters.ResultType) && Enum.TryParse<FieldType>(calculatedFieldParameters.ResultType, out FieldType resultType))
{
((FieldCalculated)f).OutputType = resultType;
}
Expand All @@ -268,7 +428,7 @@ protected override void ExecuteCmdlet()
f.Update();
ClientContext.Load(f);
ClientContext.ExecuteQueryRetry();
}
}
switch (f.FieldTypeKind)
{
case FieldType.DateTime:
Expand Down

0 comments on commit b33b5fe

Please sign in to comment.