Page load globar event

Targutai Yesugei asked on April 4, 2018 12:42

Hello!

Can't find global event for page loading event to get some info from http request. For tracking document insertion event i'm using DocumentEvents.Insert.After += Document_Insert_After; So, looking for something similar.

I found this , but when using this namespace, i can't find anything like CMSPages.OnBeforePageInit.

Thank you in advance.

Recent Answers


Dragoljub Ilic answered on April 4, 2018 13:36

Hi Targutai,

I'm not sure what you really try to accomplish here, but if you want to use 'CMSPages' events, they are place in the 'CMS.UIControls' namespace:

CMS.UIControls.CMSPage.OnBeforePageInit += CMSPage_OnBeforePageInit;

But this will work only for CMS administration pages. If you want that info on public pages, maybe you should consider to use custom macro and place it on master page (depends what info you need).

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 4, 2018 13:45 (last edited on April 4, 2018 15:09)

You kinda answering yourselft to your question:

using System;
using CMS;
using CMS.DataEngine;
using CMS.UIControls;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomInitializationModule))]

public class CustomInitializationModule : Module
{
    // Module class constructor, the system registers the module under the name "CustomInit"
    public CustomInitializationModule()
        : base("CustomInit")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();
        CMSPage.OnBeforePageInit += Before_Page_Init;
    }

    private bool Before_Page_Init(object sender, EventArgs e)
    {
        var referrer = HttpContext.Current.Request.UrlReferrer;
        var userAgent = HttpContext.Current.Request.UserAgent;
        // ...
        return true;
    }

}

P.S. I think you kinda looking for something to run code on any request i.e. global event on any request. You might take a look at this answer

0 votesVote for this answer Mark as a Correct answer

joyanta sen answered on August 9, 2018 01:18

Hi, I was trying the above code for one of my similar requirement but this event is getting called only for admin page not for non-admin pages. Could you please tell me the reason and what event to call for non-admin pages?

Thanks, Joyanta

0 votesVote for this answer Mark as a Correct answer

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