Create a new Retail Server Controller

Important

This content is archived and is not being updated. For the latest documentation, see Microsoft Dynamics 365 product documentation. For the latest release plans, see Dynamics 365 and Microsoft Power Platform release plans.

Applies To: Microsoft Dynamics AX 2012 R3

A controller in Microsoft Dynamics AX for Retail is a mapping for a commerce entity that controls CRUD behaviors and actions for a commerce entity type. Each commerce entity must have a corresponding controller. If you introduce your own entity type, you must create a new controller.

The following example demonstrates how to create a new entity and then create a controller for the new entity.

Note

You can find the sample code from this topic in the Retail SDK.

Create a new entity

For a new entity, define a class. The class must have a key field. In the following example, there are two properties called Name and Id. Id is the key field.

namespace Microsoft.Dynamics.RetailServer.Samples.Extensions
{
    using System.ComponentModel.DataAnnotations;

    public class NewEntity
    {
        public string Name { get; set; }

        [Key]
        public string Id { get; set; }
    }
}

Create a controller for the new entity

After you create the new entity, you must define a new controller for it. In the following example, the controller class extends the CommerceController class and takes the entity type and the key field of the entity type you created as parameters. There is one method called get that uses OData to return a collection of the NewEntity entities that are associated with the NewEntitiesController controller.

namespace Microsoft.Dynamics.RetailServer.Samples.Extensions
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using Retail.StoreServerServiceLibrary.ODataControllers;

    [ComVisible(false)]
    public class NewEntitiesController : CommerceController<NewEntity, string>
    {
        public override System.Linq.IQueryable<NewEntity> Get()
        {
            List<NewEntity> newEntities = new List<NewEntity>();

            for (int i = 0; i < 10; i++)
            {
                var newEntity = new NewEntity();
                newEntity.Id = "Id" + i;
                newEntity.Name = "Name" + i;

                newEntities.Add(newEntity);
            }

            return newEntities.AsQueryable();
        }
    }
}

See also

Retail Modern Point of Sale