Create a new Retail Server Controller [AX 2012]

Updated: April 29, 2014

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.

NoteNote

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

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

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


Announcements: To see known issues and recent fixes, use Issue search in Microsoft Dynamics Lifecycle Services (LCS).

Community Additions

ADD
Show: