November Happy Hour will be moved to Thursday December 5th.

How to use Session in InitialiationModule

Vote:
 

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 :)

#185701
Edited, Nov 30, 2017 8:40
Vote:
 

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

#185715
Nov 30, 2017 10:26
Vote:
 

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.

#185717
Edited, Nov 30, 2017 10:45
Vote:
 

Why do you not just use SetDefaultValue?

What is PropertyPageLocator?

#185720
Nov 30, 2017 11:09
Vote:
 

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.

#185721
Nov 30, 2017 11:15
Vote:
 

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.

#185728
Nov 30, 2017 12:02
Vote:
 

So you solved it, great! :-)

#185730
Nov 30, 2017 12:16
Vote:
 

Thank you :-)

#185756
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.