Copied to clipboard

Flag this post as spam?

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


  • Jan Kees Velthoven 25 posts 46 karma points
    Oct 26, 2010 @ 10:35
    Jan Kees Velthoven
    0

    Extending Examine by canceling the GatheringNodeData event

    I'm working on a project that is using Umbraco containing two websites. I want to make a search for one of the sites that is not indexing the other website. I've created the extension showed below.

    1. On the GatherNodeData event my extension comes in
    2. I check if I'm dealing with Content and if the path attibute is present
    3. With my custom Helper I only check if the NodeTypeAlias name mathes "CorporateHome" in the path
    4. If it isn't I cancel the event

    Here comes the big question:

    When I cancel the GatherNodeData event is this only for the current node or the whole proces of indexing?

        public class CancelIndexerOnNonCorporateContent : ApplicationBase
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="CancelIndexerOnWoningWebsite"/> class.
            /// </summary>
            public CancelIndexerOnNonCorporateContent()
            {
                ExamineManager.Instance.IndexProviderCollection["CorporateContentIndexer"].GatheringNodeData +=
                    CancelIndexerOnNonCorporateContent_GatheringNodeData;
            }

            /// <summary>
            /// Handles the GatheringNodeData event of the CancelIndexerOnNonCorporateContent control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="Examine.IndexingNodeDataEventArgs"/> instance containing the event data.</param>
            private void CancelIndexerOnNonCorporateContent_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
            {
                if (e.IndexType == "Content" && e.Node.Attribute("path") != null)
                {
                    string[] pathIds = e.Node.Attribute("path").Value.Split(',');
                    bool isInCorporatePath =
                        pathIds.Where(i => UmbracoHelper.GetNodeTypeAlias(Convert.ToInt32(i)) == "CorporateHome").Any();

                    if (!isInCorporatePath)
                    {
                        e.Cancel = true;
                    }
                }
            }
        }

     

  • Jan Kees Velthoven 25 posts 46 karma points
    Oct 26, 2010 @ 14:03
    Jan Kees Velthoven
    0

    I found a solution for the problem. The cancelation of the GatherNodeData has no effect on the indexing because it's too late. You can use the NodeIndexing event instead. Because there is no option to use the NodeFactoy in this thread you can use the umbraco.cms.businesslogic.web.Document instead. The only disadvantage of using this class is that you use the database for each query.

    So here is the extension to cancel an indexer if a certain document is nested in a certain ContentType with a givven alias.

        public class CancelIndexerOnNonCorporateContent : ApplicationBase
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="CancelIndexerOnWoningWebsite"/> class.
            /// </summary>
            public CancelIndexerOnNonCorporateContent()
            {
                ExamineManager.Instance.IndexProviderCollection["CorporateContentIndexer"].NodeIndexing +=
                    CancelIndexerOnNonCorporateContent_NodeIndexing;
            }

            /// <summary>
            /// Handles the NodeIndexing event of the CancelIndexerOnNonCorporateContent control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="Examine.IndexingNodeEventArgs"/> instance containing the event data.</param>
            private static void CancelIndexerOnNonCorporateContent_NodeIndexing(object sender, IndexingNodeEventArgs e)
            {
                if (e.IndexType == "content" && e.Fields.ContainsKey("path"))
                {
                    string[] pathIds = e.Fields["path"].Split(',');
                    bool isInCorporatePath =
                        pathIds.Where(i => GetDocumentContentTypeAlias(Convert.ToInt32(i)) == "CorporateHome").Any();

                    if (!isInCorporatePath)
                    {
                        e.Cancel = true;
                    }
                }
            }

            /// <summary>
            /// Gets the document content type alias.
            /// </summary>
            /// <param name="id">The id.</param>
            /// <returns></returns>
            private static string GetDocumentContentTypeAlias(int id)
            {
                try
                {
                    var document = new Document(id);
                    return document.ContentType.Alias;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
Please Sign in or register to post replies

Write your reply to:

Draft