0

I am trying to execute a Workflow Action "Approve" or "Reject" from a URL to an ID, is my approach correct?

I need help finishing it, how do I run this code from a URL and how do run it on any ID, I want to try first on the specific Content Item that I already created in the Sitecore Content Tree.

using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Workflows;
using Sitecore.Configuration;
using Sitecore.SecurityModel;

namespace WorkflowStateChanger
{
  public class StateChanger
  {
  // Get Master Database
  private readonly Database _sourceDatabase = Factory.GetDatabase("master");
  // Item State Awaiting Approval
  Item AwaitingApproval = Context.Database.GetItem(new ID("{46DA5376-10DC-4B66-B464-AFDAA29DE84F}"));
  // get Approve Command by ID | ID specific
  Item Approve = Context.Database.GetItem(new ID("{F744CC9C-4BB1-4B38-8D5C-1E9CE7F45D2D}"));
  // get Reject Command by ID | ID specific
  Item Reject = Context.Database.GetItem(new ID("{E44F2D64-1EED-42FF-A7DA-C07B834096AC}"));
  public void Execute()
  {
  // Get item by ID (need to specify ID)
  // /sitecore/content/WorkflowTest | {D7D61874-0715-43E0-9093-E334A267B2A3}
  Item item = Context.Database.GetItem(new ID("{D7D61874-0715-43E0-9093-E334A267B2A3}"));
  }

  //set the item state and executes actions which are in this state:
  public void MoveToStateAndExecuteActions(Item item, ID workflowStateId)
  {
  IWorkflowProvider workflowProvider = Context.Database.WorkflowProvider;
  IWorkflow workflow = workflowProvider.GetWorkflow(item);

  // Begit Edit
  item.Editing.BeginEdit();
  try
  {
  item[FieldIDs.WorkflowState] = workflowStateId.ToString();
  item.Editing.EndEdit();
  }
  catch (Exception)
  {
  //Revert the Changes
  item.Editing.CancelEdit();
  }
  }
  }
}
1
  • 1
    I don't have the ability to show this right at the moment, but the work box has a RSS feed that exposes actual URL's for Workflow Actions. You can decompile that logic and reuse those URL's elsewhere. Commented Dec 11, 2017 at 23:42

1 Answer 1

2

There are a number of ways to transition items through a workflow via web request.

Below are some options I can think of that require low effort.

Option 1 : Code Page

Save the code in a web page (.aspx) with query string parameters that are processed in the code you've provided. The query string could contain the GUIDs you have in the code sample.

Option 2 : SPE Web API

Convert the code into a PowerShell script and process using the SPE Web API. There you can also supply query string parameters.

Final Thoughts

In either case, the method you choose needs to have security wrapped around it so unauthorized users are not hammering the Url or transitioning items through a workflow without proper privileges.

Resources

2
  • is my approach correct?
    – Tony
    Commented Dec 11, 2017 at 21:47
  • One of the links I referenced describes transitioning from one workflow state to another, which includes executing the associated actions. Whether or not the code sample you provided is the best or right way I cannot say. Perhaps you can clarify your question to include more details around why you want to accomplish through a url and any links to where you found the snippet. Commented Dec 11, 2017 at 21:55

Your Answer

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

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