Changing a workflow state from API

Version: Sitecore

Include these using directives:
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Workflows;
using Sitecore.Configuration;
using Sitecore.SecurityModel;

Here is the code snippet with appropriate comments:

// getting the master database Database master = Factory.GetDatabase("master");

// getting a sample item Item itm = master.Items["/sitecore/content/home/news"];

// getting the item's workflow reference IWorkflow wf = master.WorkflowProvider.GetWorkflow(itm);

// here we need either to login as a user with appropriate permissions // to have access to workflow states/commands or disable security using(new SecurityDisabler())
{ // executing the workflow command by calling the Execute method. // it receives the ID of the command being invoked, // the item that is in the workflow. // Note that the command ID that we passed as the first parameter, // should be a valid ID of any workflow command under the current workflow state // that the item belongs to. WorkflowResult result =

wf.Execute("{0FB92663-F44B-4E24-9CF2-B6D6CE786505}", itm, "my comments", false);

// Retrieving the result of the workflow command execution Response.Write("Succeeded: " + result.Succeeded + "
Message: " + result.Message);

After the code above is succeeded, all actions that are tied to the new workflow state will be executed.