In order to trigger programmatically my pipeline batch using Data Exchange Framework, I decided to create a webapi following Boris Brodsky http://borisbrodsky.com/sitecore/sitecore-webapi-servicesapicontroller-and-jwt-token-security/ implementation for the security permits.
Inside my web api I added the code from the sitecore documentation:
Guid tenantId = ... //tenant that owns the pipeline batch
Guid pipelineBatchId = ... //pipeline batch
IEnumerable<PipelineBatch> batches = null;
batches = Sitecore.DataExchange.Context.TenantRepository.GetPipelineBatches(tenantId);
if (batches != null)
{
PipelineBatch pipelineBatch = batches.FirstOrDefault(x => x.ID == pipelineBatchId);
if (pipelineBatch != null)
{
var runner = new InProcessPipelineBatchRunner();
if (runner.Run(pipelineBatch))
{
//pipeline batch was started
}
}
}
My WebApi becomes as follow:
[AuthorizedUser(@"sitecore\ServicesAPI")]
[HttpGet]
[Route("QueryCrmConnect")]
public async Task<IHttpActionResult> QueryCrmConnect()
{
try
{
Guid tenantId = new Guid(Settings.GetSetting("Sitecore.DEF.TenantId")); //tenant DEF
Guid pipelineBatchId = new Guid(Settings.GetSetting("Sitecore.DEF.PipelineBatchId")); //pipeline batch
IEnumerable<PipelineBatch> batches = null;
batches = Sitecore.DataExchange.Context.TenantRepository.GetPipelineBatches(tenantId);
if (batches != null)
{
PipelineBatch pipelineBatch = batches.FirstOrDefault(x => x.ID == pipelineBatchId);
if (pipelineBatch != null)
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
var runner = new InProcessPipelineBatchRunner();
var response = await runner.RunAsync(pipelineBatch);
if (response)
{
runner.Stop(pipelineBatch);
return Content(HttpStatusCode.OK, "Batch eseguito con successo");
}
}
}
}
}
catch (Exception ex)
{
return InternalServerError(ex);
}
return Content(HttpStatusCode.OK, "Batch non eseguito");
}
If I run from a console application my webapi I have the following:
var hostName = Sitecore.Links.LinkManager.GetItemUrl(Context.Database.GetItem(Context.Site.ContentStartPath), new Sitecore.Links.UrlOptions() { AlwaysIncludeServerUrl = true });
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(hostName);
var content = new StringContent(
@"{ ""domain"": ""sitecore"", ""username"": ""ServicesAPI"", ""password"": ""b"" }", Encoding.UTF8, "application/json");
var authResult = client.PostAsync("sitecore/api/ssc/auth/login", content).Result;
var json = authResult?.Content?.ReadAsStringAsync()?.Result;
dynamic jsonToken = JsonConvert.DeserializeObject(json);
var token = jsonToken?.token?.Value as string;
client.DefaultRequestHeaders.Add("token", token);
var responseTask = client.GetAsync("api/CrmcConnect/QueryCrmConnect").Result;
return responseTask.IsSuccessStatusCode;
}
The issue I am experiencing is that I cannot get the pipepline batch
Sitecore.DataExchange.Context.TenantRepository.GetPipelineBatches(tenantId)
and it throws an exception with this message:
No item was found with the specified id. (tenant id: 1194d08b-1d46-4b41-9a4c-d1b0da1d4bca, tenant repository type: Sitecore.DataExchange.Repositories.Tenants.SitecoreTenantRepository).
To consume the Web Api, as reported in the post of Boris, I'm using the user: sitecore\ServicesAPI
.
In the User Manager of sitecore I checked that the user "sitecore\ServicesAPI" does not have the Admin permission.
If I check the flag property Administrator then I don't get anymore the error and it works fine.
Any suggestion on how I can expose my web api to third party in order to trigger my pipeline batch ? As other option is it ok if I check the flag Administrator in the User Manger for the user sitecore\ServicesAPI ?
Thanks in advance for any advice