Recherche…


Définit les actions et l'état des éléments dans cet état

public void MoveToStateAndExecuteActions(Item item, ID workflowStateId)
{
    Sitecore.Workflows.IWorkflowProvider workflowProvider = Item.Database.WorkflowProvider;
    Sitecore.Workflows.IWorkflow workflow = workflowProvider.GetWorkflow(item);

    // if item is in any workflow
    if (workflow != null)
    {
        using (new Sitecore.Data.Items.EditContext(item))
        {
            // update item's state to the new one
            item[Sitecore.FieldIDs.WorkflowState] = workflowStateId.ToString();
        }

        Item stateItem = ItemManager.GetItem(workflowStateId, 
            Language.Current, Sitecore.Data.Version.Latest, item.Database, SecurityCheck.Disable);

        // if there are any actions for the new state
        if (!stateItem.HasChildren)
            return;

        WorkflowPipelineArgs workflowPipelineArgs = new WorkflowPipelineArgs(item, null, null);

        // start executing the actions
        Pipeline pipeline = Pipeline.Start(stateItem, workflowPipelineArgs);
        if (pipeline == null)
            return;
        WorkflowCounters.ActionsExecuted.IncrementBy(pipeline.Processors.Count);
    }
}

Merci à ce fil

Exécution de la commande de workflow pour modifier l'état du workflow

Merci à ce super post

Si nous voulons imiter le comportement de Sitecore UI et exécuter la commande qui changera l'état du workflow, nous devons utiliser WorkflowProvider pour obtenir une instance du workflow assignée à l'élément donné et appeler la méthode Execute avec un ID de commande choisi. Cela déclenche toutes les actions définies sous le nœud de l'élément de commande, modifie l'état de l'élément et déclenche toutes les actions automatiques définies sous le nouveau nœud d'élément d'état:

public static WorkflowResult ExecuteCommand(Item item, string commandName, string comment)
{
    IWorkflow workflow = item.Database.WorkflowProvider.GetWorkflow(item);
 
    if (workflow == null)
    {
        return new WorkflowResult(false, "No workflow assigned to item");
    }
 
    WorkflowCommand command = workflow.GetCommands(item[FieldIDs.WorkflowState])
        .FirstOrDefault(c => c.DisplayName == commandName);
 
    if (command == null)
    {
        return new WorkflowResult(false, "Workflow command not found");
    }
 
    return workflow.Execute(command.CommandID, item, comment, false, new object[0]);
}


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow