Item CRUD operations

Create an item

You create items using the CreateItem of the IRestClient interface.

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

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

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:

var newsItem = new NewsDto()
{
Title = "Sample title"
};
newsItem.SetValue<string>("MyCustomFieldName", "My custom field value");
await restClient.CreateItem(newsItem);

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:

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

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

Retrieve a single item

You retrieve items using the GetItem of the IRestClient interface.

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

/// 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

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

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

/// 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:

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

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

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

Update an item

You update items using the UpdateItem of the IRestClient interface.

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

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

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:

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);

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:

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"
},
});

Delete an item

You delete items using the DeleteItem of the IRestClient interface.

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

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

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

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

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?

Next article

Taxonomies