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

Fixed Add-PnPField not supporting a ReturnType to be set for calculated field #2765

Merged
merged 3 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed issue with `Get-PnPFolder` ignoring `-Includes` parameter when passing in a specific list through `-List` [#2735](https://github.com/pnp/powershell/pull/2735)
- Fixed the handling of `-ErrorAction` so it follows the standard PowerShell behavior [#2741](https://github.com/pnp/powershell/pull/2741)
- Fixed issue with `Set-PnPContentType` not allowing you to update basic properties of a content type [#2760](https://github.com/pnp/powershell/pull/2760)
- Fixed `Add-PnPField` not supporting a ReturnType to be set for calculated fields when created on the site level [#2765](https://github.com/pnp/powershell/pull/2765)

### Contributors

Expand Down
33 changes: 27 additions & 6 deletions documentation/Add-PnPField.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +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] [-Connection <PnPConnection>]
[<CommonParameters>]
[-ClientSideComponentProperties <String>] [-AddToAllContentTypes] [-ReturnType <String>] [-Connection <PnPConnection>]
```

### Add field reference to list
Expand All @@ -31,8 +30,8 @@ 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>]
[-Connection <PnPConnection>] [<CommonParameters>]
[-ClientSideComponentId <Guid>] [-ClientSideComponentProperties <String>] [-ReturnType <String>]
[-Connection <PnPConnection>]
```

## DESCRIPTION
Expand Down Expand Up @@ -75,6 +74,13 @@ Add-PnPField -Type Choice -Choices "PnP","Parker","Sharing Is Caring" -DisplayNa

This will add a site column of type Choice (only one choice value can be chosen at the same time) called "My Test Column" with three choice values.

### EXAMPLE 6
```powershell
Add-PnPField -Type Calculated -ResultType Number -DisplayName "My Calculated Column" -InternalName "MyCalcCol" -Formula "=Today()"
```

This will add a site column of type Caulculated called "My Calculated Column" which contains todays date.

## PARAMETERS

### -AddToDefaultView
Expand Down Expand Up @@ -284,7 +290,7 @@ Accept wildcard characters: False
```

### -Type
The type of the field like Choice, Note, MultiChoice. For a complete list of field types visit https://learn.microsoft.com/dotnet/api/microsoft.sharepoint.client.fieldtype
The type of the field like Choice, Note, Calculate, MultiChoice. For a complete list of field types visit https://learn.microsoft.com/dotnet/api/microsoft.sharepoint.client.fieldtype

```yaml
Type: FieldType
Expand All @@ -298,6 +304,21 @@ 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)
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
9 changes: 9 additions & 0 deletions src/Commands/Fields/AddField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ protected override void ExecuteCmdlet()
EnsureDynamicParameters(calculatedFieldParameters);
f = CurrentWeb.CreateField<FieldCalculated>(fieldCI);
((FieldCalculated)f).Formula = calculatedFieldParameters.Formula;

if(!string.IsNullOrEmpty(calculatedFieldParameters.ResultType) && Enum.TryParse<FieldType>(calculatedFieldParameters.ResultType, out FieldType resultType))
{
((FieldCalculated)f).OutputType = resultType;
}
else
{
((FieldCalculated)f).OutputType = FieldType.Text;
}
f.Update();
ClientContext.ExecuteQueryRetry();
}
Expand Down