3

I have set up a custom button in the Content Editor that makes a third-party API call when clicked. In order to let the user know that the client is performing some work, I have implemented a ProgressBox. I would also like to alert the user at the end to any errors that may have occurred (it's not enough to tell them to check to the logs). Here is how my code is set up.

public override void Execute(CommandContext context)
{
    string message = "Result\r\n";
    Sitecore.Shell.Applications.Dialogs.ProgressBoxes.ProgressBox.Execute(
        "Push To API",
        "API Call",
        new Sitecore.Shell.Applications.Dialogs.ProgressBoxes.ProgressBoxMethod(StartProcess),
        new object[] { });

    SheerResponse.Alert(message);
}

public void StartProcess(params object[] parameters)
{
    // API call (or calls) happen here. After the API calls are finished
    // the message value gets set to reflect what happened during the 
    // process
}

My goal is to trigger the Alert Box after the StartProcess has fully completed.

However, the Execute method runs asynchronously, so the Alert Box always runs with a message of "Result ".

Another thing to note is that I've been adding progress messages to the Progress box using code like this inside the StartProcess method.

Sitecore.Context.Job.Status.Messages.Add($"There has been an error with {item.Name}");

This is not sufficient for my needs, because the ProgressBox closes immediately upon creation of the process. Thanks in advance for any assistance here.

3

You should have a method which check the status of the task. Please see below:

protected void CheckStatus()
{
    var isJobDone = JobManager.GetJobs().FirstOrDefault(j => j.Name.Equals("Push To API") && j.Status.State == JobState.Running);

    if (isJobDone != null && !isJobDone.IsDone)
    {
        SheerResponse.Timer("CheckStatus", 100);
    }
    else
    {
        Context.ClientPage.ClientResponse.Alert("Task Completed");
    }   
}

The CheckStatus method will trigger after 100 and see if the job has been completed. If not, it will "subscribe" to it and trigger again after 100. Once the job is done, it will show the alert.

UPDATE

Due to the following error

Method "CheckStatus" not found in Sitecore.Shell.Applications.ContentManager.ContentEditorForm‌​. (method: Sitecore.Web.UI.Sheer.ClientPage.Dispatch(String command))

some changes are required because the SheerResponse.Timer is expecting that the method CheckStatus is present in the ContentEditorForm. So, to fix this, the CheckStatus method needs to be modified as follows:

protected void CheckStatus()
{
    //Change the eventName here
    SheerResponse.Timer("PublishApi:checkStatus", 100);        
}

Or you can directly call the Timer in the Execute as shown below:

public override void Execute(CommandContext context)
{
    string message = "Result\r\n";
    Sitecore.Shell.Applications.Dialogs.ProgressBoxes.ProgressBox.Execute(
        "Push To API",
        "API Call",
        new Sitecore.Shell.Applications.Dialogs.ProgressBoxes.ProgressBoxMethod(StartProcess),
        new object[] { });

    SheerResponse.Timer("PublishApi:checkStatus", 100); 
}

Then, you need to have a command entry as follows:

<configuration  xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <commands>
      <command name="PublishApi:checkStatus" type="YourNamespaceAndClass, YourAssembly" />
    </commands>    
  </sitecore>
</configuration>

Now, you need to implement the new command PublishApi:checkStatus:

public override void Execute(CommandContext context)
{
    var isJobDone = JobManager.GetJobs().FirstOrDefault(j => j.Name.Equals("Push To API") && j.Status.State == JobState.Running);

    if (isJobDone != null && !isJobDone.IsDone)
    {
        SheerResponse.Timer("PublishApi:checkStatus", 100);
    }
    else
    {
        Context.ClientPage.ClientResponse.Alert("Results");
    }
}

Note that you will require to modify the code accordingly for the result message to get updated.

| improve this answer | |
  • I really like the approach being used in checking the job, but I'm having issues with the SheerResponse.Timer method. When I run the code, I get the following error. Method "CheckStatus" not found in Sitecore.Shell.Applications.ContentManager.ContentEditorForm. (method: Sitecore.Web.UI.Sheer.ClientPage.Dispatch(String command)). – Ben Mar 22 '17 at 13:46
  • Does the method CheckStatus within the same class as your Execute method? – Hishaam Namooya Mar 22 '17 at 14:04
  • Yes, it is. I saw in this blog post that SheerResponse.Timer only works inside of a Sitecore ajax call. This code is running as the command of a custom buitton, so I'm not sure if that's a Sitecore Ajax call or not. – Ben Mar 22 '17 at 14:41
  • Also if you can provide how you have create the button on Content Editor and what template did you use – Hishaam Namooya Mar 22 '17 at 17:57
  • thanks for getting back to me. I created the custom button using the same Large Button template as you did in the Core database. – Ben Mar 22 '17 at 18:01

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.