Try our conversational search powered by Generative AI!

How to use Session in InitialiationModule


Hi everyone,

I'm having an issue, that is use HttpContext.Current.Session. I want check existence of a session variable, but if it doesn't exist, my project will display "Object reference not set to an instance of an object."

This is my code.

        private void SaveProperties(object senders, ContentEventArgs e)
            var propertyPageLocator = PropertyPageLocator.GetInstance();
            var contentRepository = ServiceLocator.Current.GetInstance();

            if (e.Content is PropertyPage)
                var currentPage = e.Content as PropertyPage;
                PropertyPage cloneCurrentPage = currentPage.CreateWritableClone() as PropertyPage;

                if (HttpContext.Current.Session["RunPluginAdmin"] == null)
                    propertyPageLocator.UpdateMinMaxAcres(ref cloneCurrentPage);
                    propertyPageLocator.UpdateMinMaxAvailableSqFt(ref cloneCurrentPage);
                    contentRepository.Save(cloneCurrentPage, SaveAction.ForceCurrentVersion);

Anyone can resolve this? Thank you :)

Edited, Nov 30, 2017 8:40

A InitialiationModule run during startup of the site so you will propably not have any current session at that stage. You need to same/get that property from something else.

What do it do and when do you set the value in the session

Nov 30, 2017 10:26

Hi @Henrik Fransas,

Thanks for your answer. Let me describe the way i do that.

I created an InitializationModule to generate properties for page, the page type has some properties [Editable(false)] and only generate when created page.

And I also need create an admin plugin to generate property for all of pages in my system because before that, their properties haven't initialized yet. But when i run the plugin, it has a problem about saving page (conflict with InitializationModule). So i tried use Session to check the page is edited by plugin or user, and i get this issue.

Do you have any idea for my isse?

Thank you very much.

Edited, Nov 30, 2017 10:45

Why do you not just use SetDefaultValue?

What is PropertyPageLocator?

Nov 30, 2017 11:09

Ah because those properties dependent value user input.

PropertyPageLocator is just a module to update properties for page. You don't need to pay attention to it.

Nov 30, 2017 11:15

Oh i detected when i run plugin, session != null and when i edit page, session == null in InitializationModule :D.

Thank you beacause of your suggestion.

Nov 30, 2017 12:02

So you solved it, great! :-)

Nov 30, 2017 12:16

Thank you :-)

Dec 01, 2017 3:27
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.