1

I have created a Powershell script that involves automatic assignment of page design to an item. Below are the portions of codes I am using:

Powershell code:

Sample of powershell script that assigns the page design id to the Page Design field

C-Sharp code

C-Sharp code of the OnItemSaving event of the Page Design

My problem is that whenever I am going to end the editing (whenever I hit the OnItemSaving event), I am not getting any value for the HttpContext.Current; hence, my request is being returned.

I do not want to remove the checks for HttpContext.Current as I still want to validate whenever it's empty or not.

4
  • Is the HttpContext null on both clicking the save button and when executing via powershell? Mar 30, 2021 at 5:58
  • Why do you need the validation?
    – Gatogordo
    Mar 30, 2021 at 6:53
  • Hi @HishaamNamooya, it's only null when executing via powershell
    – Jay
    Mar 30, 2021 at 6:59
  • @Gatogordo actually the validation was added long time ago by a team member who only mentioned that it was a fix for something but I honestly couldn't track where it's applicable or when that validation is necessary. Since it's a pipeline event, I really can't afford simply removing it.
    – Jay
    Mar 30, 2021 at 7:05

1 Answer 1

2

If you are using Sitecover Powershell ISE: by default, the script executes as a background job. You can try to click on dropdown under Execute button and select "Execute in http request context" enter image description here

3
  • Man that's cool! Didn't know that option exists! :) By the way, I am actually triggering the powershell script via a scheduled task. Can I execute it the same way as clicking that "Execute in http request context" button?
    – Jay
    Mar 30, 2021 at 10:58
  • github.com/SitecorePowerShell/Console/blob/… It looks like SPE executes scheduled tasks in the context of item inherited from /sitecore/templates/Modules/PowerShell Console/PowerShell Script. I don't see an option how to re-configure it. But it should be possible to duplicate item /sitecore/system/tasks/Commands/PowerShellScriptCommand, change logic how powershell script is started by inheriting from Spe.Integrations.Tasks.ExecuteScriptTask and overriding how the session is configured.
    – Anton
    Mar 30, 2021 at 11:37
  • 1
    Will try that one, thanks @Anton!
    – Jay
    Mar 30, 2021 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.