Frontend development

Work with dynamic items

Get a dynamic item

You can get a single item using the GetItem method of the IRestClient interface.

The following code gets a press release item by its ID:

C#
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.RestSdk.Dto;
using Progress.Sitefinity.RestSdk;

namespace Renderer.Controllers
{
    public class TestController : Controller
    {
        private IRestClient restClient;

        public TestController(IRestClient restClient)
        {
            this.restClient = restClient;
        }

        public async Task<IActionResult> GetItem()
        {
            var item = await restClient.GetItem<SdkItem>(new GetItemArgs()
            {
                Type = RestClientContentTypes.Pressreleases,
                Id = "ITEM_ID",
                // Fields = "FIELDS_TO_GET" // optional
                // Provider = "PROVIDER_OF_THE_ITEM" // optional
                // Culture = "CULTURE_OF_THE_ITEM" // optional
            });

            return this.Json(item);
        }
    }
}

NOTE: You can optionally get a field of the item or get it from a particular provider or culture.

Get a collection of dynamic items

You can get a collection of items using the GetItems method of the IRestClient interface.

The following code gets all items of type press release:

C#
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.RestSdk.Dto;
using Progress.Sitefinity.RestSdk;

namespace Renderer.Controllers
{
    public class TestController : Controller
    {
        private IRestClient restClient;

        public TestController(IRestClient restClient)
        {
            this.restClient = restClient;
        }

        public async Task<IActionResult> GetItems()
        {
            var items = await restClient.GetItems<SdkItem>(new GetAllArgs()
            {
                Type = RestClientContentTypes.Pressreleases,
                // Fields = "FIELDS_TO_GET" // optional
                // Provider = "PROVIDER_OF_THE_ITEMS" // optional
                // Culture = "CULTURE_OF_THE_ITEMS" // optional
                // Skip = 5 // optional
                // Take = 5 // optional
            });

            return this.Json(items);
        }
    }
}

NOTE: You can optionally get specific fields of the press release items or get them from a particular provider or culture. You can also skip or take specified number of items in the collection.

Get a collection of dynamic items in order

You can get a collection of items using the GetItems method of the IRestClient interface and then order the collection by the value of a field.

The following code gets all items of type press release and orders it ascending by the title:

C#
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.RestSdk.Dto;
using Progress.Sitefinity.RestSdk;

namespace Renderer.Controllers
{
    public class TestController : Controller
    {
        private IRestClient restClient;

        public TestController(IRestClient restClient)
        {
            this.restClient = restClient;
        }

        public async Task<IActionResult> GetItems()
        {
            var items = await restClient.GetItems<SdkItem>(new GetAllArgs()
            {
                Type = RestClientContentTypes.Pressreleases,
                OrderBy = new[]
                {
                    new OrderBy()
                    {
                        Name = "Title",
                        Type = OrderType.Ascending,
                    },
                },
            });

            return this.Json(items);
        }
    }
}

Get a filtered collection of dynamic items

You can get a collection of items using the GetItems method of the IRestClient interface and then filter the collection by the value of one or more fields.

The following code gets all items of type press release and filters it based on its title containg two specified values:

C#
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.RestSdk.Dto;
using Progress.Sitefinity.RestSdk;
using Progress.Sitefinity.RestSdk.Filters;

namespace Renderer.Controllers
{
    public class TestController : Controller
    {
        private IRestClient restClient;

        public TestController(IRestClient restClient)
        {
            this.restClient = restClient;
        }

        public async Task<IActionResult> GetItems()
        {
            var items = await restClient.GetItems<SdkItem>(new GetAllArgs()
            {
                Type = RestClientContentTypes.Pressreleases,
                Filter = new CombinedFilter()
                {
                    Operator = CombinedFilter.LogicalOperators.Or,
                    ChildFilters = new[]
                    {
                        new FilterClause()
                        {
                            FieldName = "Title",
                            Operator = FilterClause.StringOperators.Contains,
                            FieldValue = "SOME_VALUE",
                        },
                        new FilterClause()
                        {
                            FieldName = "Title",
                            Operator = FilterClause.StringOperators.Contains,
                            FieldValue = "ANOTHER_VALUE",
                        },
                    },
                },
            });
            return this.Json(items);
        }
    }
}

Get the parent type of a dynamic item

You can get a collection of items using the GetParent method of the IRestClient interface.

The following code gets the parent type of a an item by providing the item's type:

C#
var parentType = restClient.ServiceMetadata.GetParentType("ITEM_TYPE_FULL_NAME");

Create a dynamic item

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

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

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

Create dynamic items relations

You create items using the RelateItem method of the IRestClient interface.

The following code adds specific related data item to a specified field of a dynamic item:

C#
await restClient.RelateItem<SdkItem>(new GetAllArgs()
{
    Type = RestClientContentTypes.Pressreleases,
    Id = "ITEM_ID",
    RelationName = "RELATED_FIELD_NAME",
    RelatedItemId = "RELATED_ITEM_ID"
});

Update an item

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

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

Delete an item

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

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
});
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.