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();
}
}
}
}