Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Adriano Fabri 458 posts 1601 karma points
    Jun 21, 2017 @ 13:00
    Adriano Fabri
    0

    [SOLVED] How to create a Custom Task Plugin to rebuild an Examine Index

    I wrote this plugin (based on these instructions) but it doesn't work.

    First of all I set RebuildOnAppStart =false in examinesettings.config, then I wrote this code:

    // EXAMINE
    using Examine;
    
    // SYSTEM
    using System;
    using System.Web.Configuration;
    
    // UMBRACO
    using Umbraco.Web.Mvc;
    using Umbraco.Web.WebApi;
    
    namespace AF.UmbTasks.Plugins
    {
        /// <summary>
        /// 
        /// </summary>
        [PluginController("Tasks")]
        public class ExamineController : UmbracoApiController
        {
            [System.Web.Http.HttpGet]
            public string ExamineIndexRebuild(string code)
            {
                if (WebConfigurationManager.AppSettings["AFTask_RebuildExamineIndex"] != null)
                {
                    string examineIndexToRebuild = WebConfigurationManager.AppSettings["AFTask_RebuildExamineIndex"].ToString();
    
                    try
                    {
                        //validate the querystring
                        if (code.Equals(System.Configuration.ConfigurationManager.AppSettings["code"]))
                        {
                            ExamineManager.Instance.IndexProviderCollection[examineIndexToRebuild].RebuildIndex();
    
                            string successRebuildMessage = "Examine Index successfully rebuilded: " + examineIndexToRebuild;
                            Logger.Info(typeof(ExamineController), successRebuildMessage);
                        }
                    }
                    catch (Exception ex)
                    {
                        string errorRebuildMessage = "Examine Index NOT rebuilded: " + examineIndexToRebuild;
                        Logger.Error(typeof(ExamineController), errorRebuildMessage, ex);
                    }
                }
    
                return "complete";
            }
        }
    }
    

    When I try to access the URL "/Umbraco/Tasks/Examine/ExamineIndexRebuild?code=MyCodeForSecurity" of my website, the results is a blank xml page (see the attach file)

    I don't understand if I must create my custom route (and how) and where I must configure the "code" key.

    Can anyone help me? There is another way to create a task to rebuild the examine index?

    Adriano

    enter image description here

  • Adriano Fabri 458 posts 1601 karma points
    Jun 21, 2017 @ 14:23
    Adriano Fabri
    100

    SOLVED!!!

    I changed my code with following:

    // EXAMINE
    using Examine;
    
    // SYSTEM
    using System;
    using System.Web.Configuration;
    
    // UMBRACO
    using Umbraco.Web.Mvc;
    using Umbraco.Web.WebApi;
    
    namespace AF.UmbTasks.Plugins
    {
        /// <summary>
        /// 
        /// </summary>
        [PluginController("AFTasks")]
        public class ExamineController : UmbracoApiController
        {
            [System.Web.Http.HttpGet]
            public string ExamineIndexRebuild(string code)
            {
                string rebuildResultMessage = string.Empty;
    
                try
                {
                    //validate the querystring
                    if (!String.IsNullOrEmpty(code))
                    {
                        ExamineManager.Instance.IndexProviderCollection[code].RebuildIndex();
    
                        rebuildResultMessage = "AFTask: The Examine Index successfully rebuilded (" + code + ")";
                        Logger.Info(typeof(ExamineController), rebuildResultMessage);
                    }
                    else
                    {
                        rebuildResultMessage = "AFTask: The Examine Index has not been rebuilt! The Indexer name is missing";
                        Logger.Error(typeof(ExamineController), rebuildResultMessage, new Exception("Empty or NULL Indexer Name"));
                    }
                }
                catch (Exception ex)
                {
                    rebuildResultMessage = "AFTask: The Examine Index has not been rebuilt! The \"" + code + "\" indexer does not exist";
                    Logger.Error(typeof(ExamineController), rebuildResultMessage, ex);
                }
    
                return rebuildResultMessage;
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft