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
)
[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
Same command, but in a different context. How can I add the data to the Shared Environments database when I use the Sitecore API?
[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;
}