2

I have a pipeline batch which sync external data into Sitecore items.

I extend RunPipelineBatchCommand to run as admin user, default is running as Context.User:

 protected override User GetRunAsUser()
    {
           return User.FromName(domainName + @"\" + myadminuser, true);
    }

If I run manually the pipeline batch is running perfectly.

I have a "Run Selected Pipeline Batches Command" which is used by a Scheduler Task to run daily. The problem is the pipeline batch is running like sitecore/anonymous user and not myadminuser .

How can I set Run Selected Pipeline Batches Command to be trigger as admin user and not anonymous user.

  • Hi Vlad received any update for this, or any workaround? I'm facing the same issue. – Bhavesh Rana Nov 27 '19 at 11:47
  • @BhaveshRana my answer below might be interesting for you. – Artsem Prashkovich Nov 28 '19 at 11:25
2

It happens because the Run Selected Pipeline Batches Command uses different code to run a Pipeline Batch. I prepared a custom implementation of schedule command (unfortunately the existing command was not really override-friendly, so code looks not nice, but I assume it will work):

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.DataExchange.Contexts;
using Sitecore.DataExchange.Local.Data.Items;
using Sitecore.DataExchange.Models;
using Sitecore.DataExchange.Runners;
using Sitecore.Diagnostics;
using Sitecore.Jobs;
using Sitecore.Tasks;
using System;
using System.Collections.Generic;

namespace YourNameSpace
{
    public class CustomRunSelectedPipelineBatches : Sitecore.DataExchange.Local.Tasks.RunSelectedPipelineBatches
    {

        protected override void RunPipelineBatches(
            CommandItem command,
            IEnumerable<ID> ids,
            Sitecore.DataExchange.Local.Tasks.MultiplePipelineSettings settings)
        {
            IDictionary<ID, PipelineBatch> pipelineBatches = GetPipelineBatches(command, ids);
            if (!ArePipelineBatchesValidToRun(command, pipelineBatches.Values))
            {
                Log.Error(string.Format("At least one pipeline batch assigned to command {0} is not valid to run. None of the pipeline batches will be run, per settings on the command.", (object)command.ID, (object)command.Database.Name), (object)this);
            }
            else
            {
                IPipelineBatchRunner<Job> pipelineBatchRunner = GetPipelineBatchRunner(command);
                if (pipelineBatchRunner == null)
                {
                    Log.Error(string.Format("No pipeline batch runner is available for command {0}.", command.ID), this);
                }
                else
                {
                    using (new Sitecore.Security.Accounts.UserSwitcher(GetUser()))
                    {
                        foreach (KeyValuePair<ID, PipelineBatch> keyValuePair in pipelineBatches)
                        {
                            PipelineBatchContext pipelineBatchContext =
                                this.GetPipelineBatchContext(keyValuePair.Value);
                            if (!pipelineBatchRunner.Run(keyValuePair.Value, pipelineBatchContext) &&
                                !settings.ContinueOnError)
                            {
                                Log.Info(
                                    string.Format(
                                        "An error occured when command {0} tried to run the pipeline batch {1}. No additional pipeline batches will be run, per settings on the command.",
                                        (object) command.ID, (object) keyValuePair.Key), (object) this);
                                break;
                            }
                        }
                    }
                }
            }
        }

        protected override void RunPipelineBatchesAsync(
            CommandItem command,
            IEnumerable<ID> ids,
            Sitecore.DataExchange.Local.Tasks.MultiplePipelineSettings settings)
        {
            IDictionary<ID, PipelineBatch> pipelineBatches = this.GetPipelineBatches(command, ids);
            if (!ArePipelineBatchesValidToRun(command, pipelineBatches.Values))
            {
                Log.Error(string.Format("At least one pipeline batch assigned to command {0} is not valid to run. None of the pipeline batches will be run, per settings on the command.", (object)command.ID, (object)command.Database.Name), (object)this);
            }
            else
            {
                IPipelineBatchRunner<Job> pipelineBatchRunner = GetPipelineBatchRunner(command);
                if (pipelineBatchRunner == null)
                {
                    Log.Error(string.Format("No pipeline batch runner is available for command {0}.", command.ID), this);
                }
                else
                {
                    foreach (PipelineBatch pipelineBatch in pipelineBatches.Values)
                    {
                        object[] objArray = new object[3]
                        {
                            pipelineBatchRunner,
                            GetUser(),
                            pipelineBatch
                        };
                        JobOptions jobOptions = new JobOptions(pipelineBatch.Name, "Data Exchange", Sitecore.Context.Site.Name, (object)this, "CustomRunPipelineBatchAsJob", objArray);
                        pipelineBatchRunner.CurrentProcesses[pipelineBatch.Identifier] = JobManager.Start(jobOptions);
                    }
                }
            }
        }

        public void CustomRunPipelineBatchAsJob(IPipelineBatchRunner<Job> runner, Sitecore.Security.Accounts.User currentUser, PipelineBatch pipelineBatch)
        {
            if (runner == null || pipelineBatch == null)
                return;
            using (new Sitecore.Security.Accounts.UserSwitcher(currentUser))
            {
                PipelineBatchContext pipelineBatchContext = this.GetPipelineBatchContext(pipelineBatch);
                runner.Run(pipelineBatch, pipelineBatchContext);
            }

        }

        private IPipelineBatchRunner<Job> GetPipelineBatchRunner(
            CommandItem command)
        {
            if (command == null)
                return null;
            BaseRunMultiplePipelineBatchesCommandItem innerItem = (BaseRunMultiplePipelineBatchesCommandItem)command.InnerItem;
            if (innerItem == null)
            {
                Log.Error(string.Format("The command {0} cannot run pipeline batches.", command.ID), this);
                return null;
            }
            Type pipelineBatchRunnerType = innerItem.PipelineBatchRunnerType;
            if (!(pipelineBatchRunnerType == null))
                return Activator.CreateInstance(pipelineBatchRunnerType) as IPipelineBatchRunner<Job>;
            Log.Error(string.Format("The command {0} does not have a valid pipeline batch runner type specified.", command.ID), this);
            return null;
        }

        private bool ArePipelineBatchesValidToRun(
            CommandItem command,
            IEnumerable<PipelineBatch> pipelineBatches)
        {
            foreach (PipelineBatch pipelineBatch in pipelineBatches)
            {
                if (pipelineBatch == null)
                    return false;
                IPipelineBatchRunner<Job> pipelineBatchRunner = GetPipelineBatchRunner(command);
                if (pipelineBatchRunner == null)
                    return false;
                if (pipelineBatchRunner.IsRunning(pipelineBatch.Identifier))
                {
                    Log.Error(string.Format("At least one pipeline batch assigned to command {0} is already running.", (object)command.ID, (object)command.Database.Name), (object)this);
                    return false;
                }
            }
            return true;
        }

        private IDictionary<ID, PipelineBatch> GetPipelineBatches(
            CommandItem command,
            IEnumerable<ID> ids)
        {
            Dictionary<ID, PipelineBatch> dictionary = new Dictionary<ID, PipelineBatch>();
            Sitecore.DataExchange.IConverter<Item, PipelineBatch> converter = this.Converter;
            if (converter == null)
            {
                Log.Error(string.Format("No converter is assigned to convert items to pipeline batch objects. (command: {0}, database: {1})", (object)command.ID, (object)command.Database.Name), (object)this);
                return (IDictionary<ID, PipelineBatch>)dictionary;
            }
            foreach (ID id in ids)
            {
                Item source = command.Database.GetItem(id);
                if (source == null)
                {
                    Log.Error(string.Format("Item not found in the database. (command: {0}, database: {1}, id: {2})", (object)command.ID, (object)command.Database.Name, (object)id), (object)this);
                }
                else
                {
                    Sitecore.DataExchange.ConvertResult<PipelineBatch> convertResult = converter.Convert(source);
                    if (convertResult.WasConverted)
                    {
                        PipelineBatch convertedValue = convertResult.ConvertedValue;
                        if (convertedValue == null)
                            Log.Error(string.Format("Item could not be converted to a pipeline batch. (command: {0}, database: {1}, id: {2})", (object)command.ID, (object)command.Database.Name, (object)id), (object)this);
                        else
                            dictionary.Add(id, convertedValue);
                    }
                }
            }
            return (IDictionary<ID, PipelineBatch>)dictionary;
        }


        protected virtual Sitecore.Security.Accounts.User GetUser()
        {
            return Sitecore.Security.Accounts.User.FromName(domainName + @"\" + myadminuser, true);
        }
    }
}

Put it into your solution build and set according namespace for the schedule command.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.