# Dynamic content and custom fields

## Overview

For overall information about all the modules and their properties you can use the `sfmeta` request in the following way:

`{baseurl}/api/default/sfmeta`

**Sample request**

> success  **GET** `http://mysite.com/api/default/sfmeta`

Working with dynamic contents or custom fields is similar to all other modules and their default properties. The following examples use the dynamic content module `Press releases` with various custom fields:

## Create a dynamic content item with custom fields

To create a dynamic item, you must execute a `POST` request to the following endpoint:

`{baseurl}/api/default/{entity}`

Where `{entity}` is the name of the dynamic module that you are working with.

You must specify all the properties of the item in `JSON` format inside the request body.

**Sample request**

> success  **POST** <http://mysite.com/api/default/pressreleases>

<SfDetails summary="Request body">

```JSON
{
    "Title": "Sample press release",
    "SampleLongText": "Some long text",
    "SampleShortText": "Some short text",
    "SampleRadioButtonsChoices": "2",
    "SampleDropdownListChoices": "4",
    "SampleMultipleChoices": "3",
    "SampleYesNo": true,
    "SampleDateAndTime": "2021-05-26T21:00:00Z",
    "SampleNumber": 5
}
```

</SfDetails>

**Sample response**

<SfDetails summary="Status code: 201 Created">

```JSON
{
    "@odata.context": "http://mysite.com/api/default/$metadata#pressreleases/$entity",
    "Id": "f22ba36f-6987-4729-8509-200caf92beb1",
    "LastModified": "2021-05-27T17:06:43Z",
    "PublicationDate": "2021-05-27T17:06:43Z",
    "DateCreated": "2021-05-27T17:06:43Z",
    "IncludeInSitemap": true,
    "UrlName": "bc680c36-c7d1-46cc-911c-7437584ce9b9",
    "SampleNumber": 5,
    "SampleYesNo": true,
    "SampleRadioButtonsChoices": "2",
    "SampleDropdownListChoices": "4",
    "SampleMultipleChoices": "3",
    "SampleShortText": "Some short text",
    "SampleDateAndTime": "2021-05-26T21:00:00Z",
    "Title": "Sample press release",
    "SampleLongText": "Some long text",
    "Provider": "OpenAccessProvider"
}
```

</SfDetails>

## Get a dynamic content item with custom fields

To get a single item, you must execute a `GET` request to the following endpoint:

`{baseurl}/api/default/{entity}({itemId})`

Where:

* `{entity}` is the name of the dynamic module that you are working with
* `{itemId}` is the ID of the item

**Sample request**

> success  **GET** `http://mysite.com/api/default/pressreleases(2fba96fb-c69b-4cab-8741-12d78da207cd)`

**Sample response**

<SfDetails summary="Status code: 200 OK">

```JSON
{
    "@odata.context": "http://mysite.com/api/default/$metadata#pressreleases(*)/$entity",
    "Id": "2fba96fb-c69b-4cab-8741-12d78da207cd",
    "LastModified": "2021-05-27T16:58:01Z",
    "PublicationDate": "2021-05-27T16:58:01Z",
    "DateCreated": "2021-05-27T16:58:01Z",
    "IncludeInSitemap": true,
    "UrlName": "sample-press-release",
    "SampleNumber": 5,
    "SampleYesNo": true,
    "SampleRadioButtonsChoices": "2",
    "SampleDropdownListChoices": "4",
    "SampleMultipleChoices": "3",
    "SampleShortText": "Some short text",
    "SampleDateAndTime": "2021-05-26T21:00:00Z",
    "Title": "Sample press release",
    "SampleLongText": "Some long text",
    "Provider": "OpenAccessProvider"
}
```

</SfDetails>

## Update a dynamic content item with custom fields

To update a dynamic item, you must execute a `PATCH` request with the properties that you want to modify to the following endpoint:

`{baseurl}/api/default/{entity}({itemId})`

Where:

* `{entity}` is the name of the dynamic module that you are working with
* `{itemId}` is the ID of the dynamic content item

You must specify all properties that you want to modify in `JSON` format inside the request body.

**Sample request**

> success  **PATCH** `http://mysite.com/api/default/pressreleases(f22ba36f-6987-4729-8509-200caf92beb1)`

<SfDetails summary="Request body">

```JSON
{
    "Title": "Updated sample press release",
    "SampleLongText": "Updated long text",
    "SampleShortText": "Updated short text",
    "SampleRadioButtonsChoices": "4",
    "SampleDropdownListChoices": "2",
    "SampleMultipleChoices": "6",
    "SampleYesNo": false,
    "SampleDateAndTime": "2021-05-30T07:30:00Z",
    "SampleNumber": 10
}
```

</SfDetails>

**Sample response**

<SfDetails summary="Status code: 204 No Content">

No response body

</SfDetails>

## Delete a dynamic content item with custom fields

To delete a dynamic item, execute a `DELETE` request to the following endpoint:

`{baseurl}/api/default/{entity}({itemId})`

Where:

* `{entity}` is the name of the dynamic module that you are working with
* `{itemId}` is the ID of the dynamic content item

**Sample request**

> success  **DELETE** `http://mysite.com/api/default/pressreleases(f22ba36f-6987-4729-8509-200caf92beb1)`

**Sample response**

<SfDetails summary="Status code: 204 No Content">

No response body

</SfDetails>

## Work with the Choice field

When working with a choice field through the OData web services, instead of the selected textual value of the field, such as *Option1* or *Option2*, the field value is returned as a number – for example, *2* or *4*.

The returned value indicates which choice option has been selected. It is returned as two to the power of the position of that choice in the collection of all choices.

> success **EXAMPLE**: If you have configured your choice field with 4 options - *Option 1*, *Option 2*, *Option 3*, and *Option 4*, the value that will be returned by the service is:
>
> * If the selected option is *Option 1*, the service returns 1 (2 to the power of 0)
> * If the selected option is *Option 2*, the service returns 2 (2 to the power of 1)
> * If the selected option is *Option 3*, the service returns 4 (2 to the power of 2)
> * If the selected option is *Option 4*, the service returns 8 (2 to the power of 3)
> * If it is a multi-select choice field and the selected options are Option 1, Option 2 and Option 3, the service returns 7 (the sum of the values for Option 1, Option 2 and Option 3)

When you filter a collection by the value of the choice field, have in mind that the `CLR` type to which a choice field is being resolved in the OData implementation is `Enum`. To filter by the value of a choice field, you must first use a `cast()` operation in the query string. This way, you cast the field's type to `Edm.String`.

To filter a collection by the value of a selected choice, execute a `GET` request to the following endpoint:

`{baseurl}/api/default/{entity}?$filter=cast({relatedDataFieldName}, 'Edm.String') eq {filterValue}`

Where:

* `{entity}` is the entity of the module you are working with
* `{relatedDataFieldName}` is the name of the related choice field
* `{filterValue}` is the value that you want to filter the collection by

**Sample request (filter by value)**

> success  **GET** `http://mysite.comapi/default/pressreleases?$filter=cast(SampleRadioButtonsChoices, 'Edm.String') eq '1'&$count=true`

**Sample response**

<SfDetails summary="Status code: 200 OK">

```JSON
{
    "@odata.context": "http://mysite.com/api/default/$metadata#pressreleases(Id,LastModified,PublicationDate,DateCreated,IncludeInSitemap,UrlName,SampleNumber,SampleYesNo,SampleRadioButtonsChoices,SampleDropdownListChoices,SampleMultipleChoices,SampleShortText,SampleDateAndTime,Title,SampleLongText)",
    "@odata.count": 1,
    "value": [
        {
            "Id": "4c81a30e-f312-49d4-8556-1d0bb3e836d2",
            "LastModified": "2021-05-27T17:30:09Z",
            "PublicationDate": "2021-05-27T17:18:47Z",
            "DateCreated": "2021-05-27T17:18:47Z",
            "IncludeInSitemap": true,
            "UrlName": "sdfdsf",
            "SampleNumber": null,
            "SampleYesNo": false,
            "SampleRadioButtonsChoices": "1",
            "SampleDropdownListChoices": "1",
            "SampleMultipleChoices": "1",
            "SampleShortText": "",
            "SampleDateAndTime": null,
            "Title": "Press release 1",
            "SampleLongText": ""
        }
    ]
}
```

</SfDetails>

**Sample request (filter by inequality)**

To filter a collection of dynamic items by the value of a choice field not equal to a specific value, use the following `GET` request:

> success  **GET** `http://mysite.comapi/default/pressreleases?$filter=cast(SampleRadioButtonsChoices, 'Edm.String') ne '1'&$count=true`

**Sample response**

<SfDetails summary="Status code: 200 OK">

```JSON
{
    "@odata.context": "http://mysite.com/api/default/$metadata#pressreleases(Id,LastModified,PublicationDate,DateCreated,IncludeInSitemap,UrlName,SampleNumber,SampleYesNo,SampleRadioButtonsChoices,SampleDropdownListChoices,SampleMultipleChoices,SampleShortText,SampleDateAndTime,Title,SampleLongText)",
    "@odata.count": 2,
    "value": [
        {
            "Id": "5a12fdb6-4766-42d2-8450-9da631d9c95b",
            "LastModified": "2021-05-27T17:30:02Z",
            "PublicationDate": "2021-05-27T17:30:02Z",
            "DateCreated": "2021-05-27T17:30:02Z",
            "IncludeInSitemap": true,
            "UrlName": "press-release-2",
            "SampleNumber": null,
            "SampleYesNo": false,
            "SampleRadioButtonsChoices": "2",
            "SampleDropdownListChoices": "2",
            "SampleMultipleChoices": "2",
            "SampleShortText": "",
            "SampleDateAndTime": null,
            "Title": "Press release 2",
            "SampleLongText": ""
        },
        {
            "Id": "94c7b9b8-f59e-4a0d-b5c7-1aa0d0a031e8",
            "LastModified": "2021-05-27T17:30:21Z",
            "PublicationDate": "2021-05-27T17:30:21Z",
            "DateCreated": "2021-05-27T17:30:21Z",
            "IncludeInSitemap": true,
            "UrlName": "press-release-3",
            "SampleNumber": null,
            "SampleYesNo": false,
            "SampleRadioButtonsChoices": "4",
            "SampleDropdownListChoices": "4",
            "SampleMultipleChoices": "4",
            "SampleShortText": "",
            "SampleDateAndTime": null,
            "Title": "Press release 3",
            "SampleLongText": ""
        }
    ]
}
```

</SfDetails>
