-1

I'm working with Sitecore 9.2 , And I need to create a custom button inside custom ribbon when click on button open dialog so the content author can select date and then i will do my business function after that

  • first i created the ribbon
  • then i created the button
  • i created the command class which will contain the powershell code and my business function

the problem that the powershell script didn't executed , i need to know how to write powershell inside C# class and why my code didn't executed

public override void Execute(CommandContext context){
using (ScriptSession scriptSession = ScriptSessionManager.NewSession("Default", true)){ 
var command = "$result = Read-Variable -Parameters ` @{ Name=\"from\"; Value=[System.DateTime]::Now.AddDays(-5); Title=\"Start Date\"; Tooltip=\"Date since when you want the report to run\"; Editor=\"date time\"}` -Description \"This Dialog shows less editors, it doesn't need tabs as there is less of the edited variables\" `" +
" -Title \"Initialise various variable types (without tabs)\" -Width 500 -Height 480 -OkButtonName \"Proceed\" -CancelButtonName \"Abort\"  ";
List<object> results;
results = scriptSession.ExecuteScriptPart(command, false);
}
}

i found this error in the SPE log

Exception: System.InvalidOperationException Message: Non interactive session cannot perform an interactive operation requested by the 'Read-Variable' command.

5
  • 1
    you can add a button to the ribbon without C# code. wouldn't it be enough for you? doc.sitecorepowershell.com/modules/integration-points/… – Tamás Tárnok Oct 27 '20 at 12:35
  • 3
    Definitely overdoing things here. There are integration points that allow you to tap into the various features of the Content Editor with only items and PowerShell. – Michael West Oct 27 '20 at 12:47
  • 1
    Seems kind of counter-intuitive to go the effort of creating a button in C# and then not just writing the code in C#.... – jammykam Oct 27 '20 at 15:10
  • Thank you @TamásTárnok i will try this option, if it's worked it enough for sure – Mohamed saad Oct 27 '20 at 18:55
  • thank you @MichaelWest i will try powershell integration – Mohamed saad Oct 27 '20 at 18:57
0

As per suggestion from @Tamás Tárnok and @Michael West ,it's easy way to create ribbon button using Powershell without any C# code

this Document is helpful powershell integration Points

and this link step by step to create ribbon button using powershell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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