Wednesday, May 25, 2016

Custom Processors in Sitecore


If the processor has a method, the method must accept a PipelineArgs object and return type will be void. Below code snippet will help us to write simple custom processor in Sitecore.


 public void Process(AverageRatingProcessorArguments args)
        {

            double average = 0;
            String ConStringName = Sitecore.Configuration.Settings.GetSetting("ratingconstring");

            using (SqlConnection connection = new SqlConnection(ConStringName))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("Getaverage", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@BlogItemID", args.itemID);
                    average = (double)command.ExecuteScalar();
                }
            }
       
            args.CustomData.Add("averagerating", Convert.ToString(average));
        }


 public class AverageRatingProcessorArguments:PipelineArgs
    {
        public string itemID { get; set; }

    }

//Invoking the custom processor
public string AverageRatingWithProcessor(string itemID)
        {
             
            RatingModule.Processors.AverageRatingProcessorArguments ARarg = new RatingModule.Processors.AverageRatingProcessorArguments();
            ARarg.itemID = itemID;          
           
            CorePipeline.Run(AVERAGE_RATING_PROCESSOR, ARarg);
            var AvgRating = ARarg.CustomData["averagerating"];

            return AvgRating.ToString();
        }

No comments:

Post a Comment