# Item CRUD operations

## Create an item

You create items using the `CreateItem` of the `IRestClient` interface.

<SfDetails summary="Create a static item">

The following example create a news item with title `Sample` title:

```C#
await restClient.CreateItem(new NewsDto()
{
    Title = "Sample title"
});
```

</SfDetails>

<SfDetails summary="Create a static item and set the value its custom field">

You can set a custom field for built-in types using the `SetValue` method.\
The following code creates a news item with title `Sample title` and sets the value of its `MyCustomFieldName` field to `My custom field value`:

```C#
var newsItem = new NewsDto()
{
    Title = "Sample title"
};

newsItem.SetValue<string>("MyCustomFieldName", "My custom field value");

await restClient.CreateItem(newsItem);
```

</SfDetails>

<SfDetails summary="Create a dynamic item and set the value its custom field">

The following code creates a press release item with title Sample title and sets the value of its `MyCustomFieldName` field to `My custom field value`:

```C#
var item = await restClient.CreateItem<SdkItem>(new CreateArgs()
{
    Type = RestClientContentTypes.Pressreleases,
    Data = new
    {
        Title = "Sample title",
        MyCustomFieldName = "My custom field value"
    },
}, /* optional providerName parameter */);
```

> info **NOTE**: You can optionally provide a provider name, which will create the item in a specified provider.

</SfDetails>

## Retrieve a single item

You retrieve items using the `GetItem` of the `IRestClient` interface.

<SfDetails summary="Retrieve an item with all of its fields">

You can retrieve a single item, using one of the following methods:

```C#
/// Gets a single item.
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="restClient">The rest client.</param>
/// <param name="id">The id of the item.</param>
/// <param name="culture">The culture, in which to get the item.</param>
/// <param name="provider">The provider of the item.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>

public static Task<T> GetItem<T>(this IRestClient restClient, string id)
    where T : class, ISdkItem

public static Task<T> GetItem<T>(this IRestClient restClient, string id, string provider)
    where T : class, ISdkItem

public static Task<T> GetItem<T>(this IRestClient restClient, string id, string provider, string culture)
    where T : class, ISdkItem
```

> info  **NOTE**: These methods retrieve all of the fields of the item - both plain and related.

</SfDetails>

<SfDetails summary="Retrieve an item with a selection of its fields">

The `IRestClient` interface has a method that allows more granular control over the retrieved item’s fields:

```C#
 /// Gets a single item.
 /// <typeparam name="T">The type of the item.</typeparam>
 /// <param name="args">The arguments to be passed to the service.</param>
 /// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
 Task<T> GetItem<T>(GetItemArgs args)
     where T : class;
```

The `GetItemArgs` argument holds properties that enable more granular control over the fetching of an item like in the following example:

```C#
restClient.GetItem<NewsDto>(new GetItemArgs()
{
    Type = RestClientContentTypes.News,
    Provider = "random provider",
    Id = item.Id,
    Fields = new [] { "Title" },
    Culture = "en",
});
```

> info **NOTE**: the Type property is not required, if the generic argument (in this case `<NewsDto>`) is decorated with the `MapppedSitefinityContentTypeAttribute`.

> success  **EXAMPLE**: The following example retrieves a single news item by passing an id only:\
> `restClient.GetItem(Guid.NewGuid().ToString());`

</SfDetails>

## Update an item

You update items using the `UpdateItem` of the `IRestClient` interface.

<SfDetails summary="Update a static item">

The following code updates the title of the news item with the specified ID to `Sample title`:

```C#
await restClient.UpdateItem(new NewsDto() 
{
    Id = "ID_OF_THE_ITEM",
    // Privider = "PROVIDER_OF_THE_ITEM" //optional
    Title = "Sample title" 
});
```

</SfDetails>

<SfDetails summary="Update a static item with custom fields">

You can set a custom field for built-in types using the `SetValue method`.\
The following code updates the title of the news item with the specified ID to `Sample title` and the value of its `MyCustomFieldName` field to `My custom field value`:

```C#
var newsItem = new NewsDto() 
{
    Id = "ID_OF_THE_ITEM",
    // Privider = "PROVIDER_OF_THE_ITEM" //optional
    Title = "Sample title"
};

newsItem.SetValue<string>("MyCustomFieldName", "My custom field value");

await restClient.UpdateItem(newsItem);
```

</SfDetails>

<SfDetails summary="Update a dynamic item with custom fields">

The following code updates the title of the press release with the specified ID to `Sample title` and the value of its `MyCustomFieldName` field to `My custom field value`:

```C#
await restClient.UpdateItem(new UpdateArgs()
{
    Type = RestClientContentTypes.Pressreleases,
    Data = new
    {
        Id = "ID_OF_THE_ITEM",
        // Privider = "PROVIDER_OF_THE_ITEM" //optional
        Title = "Sample title",
        MyCustomFieldName = "My custom field value"
    },
});
```

</SfDetails>

## Delete an item

You delete items using the `DeleteItem` of the `IRestClient` interface.

<SfDetails summary="Delete a static item">

The following code deletes the news item with the specified ID:

```C#
await restClient.DeleteItem(new NewsDto()
{
    Id = "ID_OF_THE_ITEM",
    // Privider = "PROVIDER_OF_THE_ITEM" //optional
});
```

</SfDetails>

<SfDetails summary="Delete a dynamic item">

The following code deletes the press release with the specified ID:

```C#
await restClient.DeleteItem(new DeleteArgs()
{
    Type = RestClientContentTypes.Pressreleases,
    Id = item.Id,
    // Privider = "PROVIDER_OF_THE_ITEM" //optional
});
```

</SfDetails>
