Custom Module or Something Else?

Jim Piller asked on January 23, 2021 07:39

Hello,

I have a custom page type in an existing site that has a custom drop down control. When the user saves this page type, the ID's of the items in the list are saved to the database. The user has since come back and said they want to make the text searchable on the site search, so I need to add the content to the search index. My thought on how to do this was to add a text field to the page type and then use a global event to update the table w/ the text values from the drop down. I've tried wiring up a custom module for this, but so far I'm not having any luck getting the event to fire. I'm wondering if custom modules is the correct path or if I should use something else? I know we're not supposed to edit the database directly, so I don't want to use a trigger in SQL, but if I can't figure this out, I might just have to.

Any help is appreciated!

Thanks

Recent Answers


Dmitry Bastron answered on January 24, 2021 14:00

Hi Jim,

You are right, you need a custom module so that you can hook into GetContent document event. Refer to Example on this documentation page, I think it explains exactly your case.

0 votesVote for this answer Mark as a Correct answer

Sean Wright answered on January 25, 2021 05:29

Jim,

Does your custom module class exist in the CMSApp project or in a separate project?

if it's in a separate project you need to make sure the [assembly: AssemblyDiscoverable] attribute exists somewhere in the project code.

I agree with Dmitry, a custom module class to hook into the search indexing events, is the best approach.

0 votesVote for this answer Mark as a Correct answer

Jim Piller answered on January 25, 2021 14:47

Hey Guys,

Thanks for the great answers and I will dig into this for sure! I don't think I was clear on my original question however. The field that I need to add to the index doesn't currently have any data in it. I need to fill in that data when a particular page type is either saved or updated. In my case, it's an Attorney page type.

I have tried wiring up the custom class module in both the WebApp.sln and a separate code project, making sure that I have the Assembly Discoverable in the AssemblyInfo.cs per the documentation. However, I don't think the event is firing for some reason and I don't know why. Below is the code that I have to this point:

[assembly: RegisterModule(typeof(SaveAttorneyOverride))] namespace MHCustomModules.SaveAttorneyAdmissionsCourts { public class SaveAttorneyOverride : Module { public SaveAttorneyOverride() : base("JimsTest") {

    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

        // Assigns custom handlers to events
        DocumentEvents.Insert.After += Document_Insert_After;
        DocumentEvents.Update.After += Document_Update_After;
    }

    private void Document_Insert_After(object sender, DocumentEventArgs e)
    {
        // Add code to add in the text value here               
    }

    private void Document_Update_After(object sender, DocumentEventArgs e)
    {
        EventLogProvider.LogInformation("Page Update Test", "Attorney Updated");
    }
}

}

As you can see, I've even tried just adding some logging to the document update method and I don't see my log updates in the Kentico log. Any help would be greatly appreciated. Thanks!

0 votesVote for this answer Mark as a Correct answer

Sean Wright answered on January 25, 2021 15:51

Jim,

There is a scheduled task that ensures search index tasks are executed. Is that scheduled task enabled? Do you see search index tasks listed in the Smart Search module?

You could also try creating a search index task yourself, manually, and see if that triggers the event.

SearchTaskInfoProvider.CreateTask(new SearchTaskCreationParameters
{
    TaskType = SearchTaskTypeEnum.Update,
    ObjectType = TreeNode.OBJECT_TYPE,
    ObjectField = SearchFieldsConstants.ID,
    TaskValue = node.GetSearchID(),
    RelatedObjectID = node.DocumentID
});
0 votesVote for this answer Mark as a Correct answer

Jake Kula answered on January 25, 2021 21:13

Hi Jim,

Is this a Web Application or a Website? If a Web Application, can you rebuild and try again?

If you debug in VS, are you successfully able to step into the OnInit() method? If so, can you step into the Document_Update_After() method?

Also, I assume you may have used this guide? https://docs.xperience.io/k12sp/custom-development/handling-global-events

Tricky one...

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on January 26, 2021 12:21

Hi Jim,

Maybe also a stupid question, but when you say "when a particular page type is either saved or updated" - do you mean page type edit (edited in Page Types application, change page type settings) OR page edit of this page type (edited in Pages application, basically, amend the content)?

The code example we provided are all referring to the second option when you edit content in Pages application, not the Page Type itself.

0 votesVote for this answer Mark as a Correct answer

Jim Piller answered on January 26, 2021 14:31

Hey Guys,

Sorry didn't get back to this yesterday. All great questions: Dimitri, what I meant was page edit of this page type. Jake, I have tried debugging both by attaching to process and also by running the WebApp.sln and I can't get the debugging to work, so I haven't been able to confirm that I'm getting into the OnInit method or the page edit / update method, which is why we added in just statement to write something to the event log in Kentico.

Thanks for all the great feedback so far, I really appreciate it! Hopefully today I'll get back to this and see if I can make progress.

Thanks,

Jim P.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on January 26, 2021 14:58

Hi Jim,

Ok, plot thickens :)

What version of Kentico are you using? Is it MVC or Portal Engine? If it's MVC, have you implemented your module for both CMS and MVC applications, and what application are you debugging actually - CMS or MVC?

0 votesVote for this answer Mark as a Correct answer

Jim Piller answered on January 26, 2021 15:37

It's an MVC application, Kentico 13 initial version. I was trying to debug the CMS application. I can debug the MVC application just fine. Do I need to put the code in both the CMS and MVC applications if the data isn't going to be displayed on the website? I've read through the documentation about 100 times and it doesn't seem like I need it in the MVC application unless I'm going to put the data on the MVC site, which I'm not.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on January 26, 2021 17:09

Ok, I've quickly looked at the released hotfixes and there might be a fixed bug in version 13. Check out 13.0.5: Search - Page updates not reflected in Azure search indexes. Update to the latest hotfix and try debugging again.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.