0

I made a Sitecore API and registered a Router, and after that, I called it through PostmanAPI. I write a command process to add data to the database but follow the default when inheriting from CommerceODataController. The default environment is global, with CommerceContext being the CurrentContext. As expected, the data will be in the Shared Environments database, but it is in the Global database (I use Sitecore 10.1. Maybe not much different compared with Sitecore 9)

enter image description here

[HttpPut]
    [ODataRoute("PostNoteSettingCommand()", RouteName = "api")]
    public async Task<IActionResult> SampleCommand([FromBody] ODataActionParameters value)
    {
        if (!ModelState.IsValid || value == null)
        {
            return new BadRequestObjectResult(ModelState);
        }

        if (!value.ContainsKey("MyName") || string.IsNullOrEmpty(value["MyName"]?.ToString()))
        {
            return new BadRequestObjectResult(value);
        }

        var name = value["MyName"].ToString();
        var displayName = value["DisplayName"].ToString();
        var message = value["Message"].ToString();
        var entity = value["Entity"].ToString();
        var discount = "0";
        var feedback = "";

        var objectProcess = new CreateSettingArg(name, displayName, message, discount, entity, feedback);

        var command = Command<CreateSettingCommand>();


        CurrentContext.AddPolicyKeys("AddSetting");


        var result = await command.Process(CurrentContext, objectProcess).ConfigureAwait(false);

        return new ObjectResult(result);
    }

CreateSettingCommand

 public async Task<Setting> Process(CommerceContext commerceContext, CreateSettingArg arg)
    {
        using (var activity = CommandActivity.Start(commerceContext, this))
        {
            var setting = new Setting
            {
                Id = arg.Name.ToEntityId<Setting>(),
                DisplayName = arg.DisplayName,
                Name = arg.Name,
                IsApprove = false,
                Message = arg.Message,
                Discount = arg.Discount,
                Feedback = arg.Feedback
            };

            var listMemberShipComponent = setting.GetComponent<ListMembershipsComponent>();
            if (!listMemberShipComponent.Memberships.Contains(CommerceEntity.ListName<Setting>()))
            {
                listMemberShipComponent.Memberships.Add(CommerceEntity.ListName<Setting>());
                setting.SetComponent(listMemberShipComponent);
            }

            var transientListMembershipComponent = setting.GetComponent<TransientListMembershipsComponent>();
            if (!transientListMembershipComponent.Memberships.Contains(CommerceEntity.ListName<Setting>()))
            {
                transientListMembershipComponent.Memberships.Add(CommerceEntity.ListName<Setting>());
                setting.SetComponent(transientListMembershipComponent);
            }

            var persistResult = await _commerceCommander.PersistEntity(commerceContext, setting);

            return persistResult
                ? setting
                : null;
        }
    }

When I create a custom plugin in Business Tools, the command processor will use context.CommerceContext and data will be added to the Shared Environments database

enter image description here

Same command, but in a different context. How can I add the data to the Shared Environments database when I use the Sitecore API?

enter image description here

[PipelineDisplayName("DoActionAddSetting")]
public class DoActionAddSetting : AsyncPipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext>
{
    private readonly CommerceCommander _commerceCommander;

    public DoActionAddSetting(
        CommerceCommander commerceCommander)
    {
        this._commerceCommander = commerceCommander;
    }

    public override async Task<EntityView> RunAsync(EntityView entityView, CommercePipelineExecutionContext context)
    {
        if (entityView == null
            || !entityView.Action.Equals(context.GetPolicy<SettingsUiPolicy>().AddSettingActionName, StringComparison.OrdinalIgnoreCase))
        {
            return entityView;
        }
3
  • Please update your question so your code examples are in code blocks instead of screenshots. Jun 3, 2022 at 22:50
  • I never face this issue but Shared DB is configured in Plugin.SQL.PolicySet-1.0.0.json file. Check the configuration if it is pointing to correct DB. Jun 4, 2022 at 7:06
  • I had added code to the question.
    – nhannt201
    Jun 5, 2022 at 8:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy