sitecoresitecore6sitecore-workflow

How do I programmatically send an item to workflow?


I have a "project" template, a workflow "project workflow" on that template and a lot of items created from the project template.

As a non-admin user, when I edit a project item it gets sent to workflow. That doesn’t happen if I'm logged in as admin, because admin users ignore workflow.

I have a bulk import process where I edit items via the Sitecore API:

updateItem.Editing.BeginEdit();
updateItem.Versions.AddVersion();
updateItem.Fields["x"] = someVal;
updateItem.Fields["y"] = someOtherVal;
updateItem.Fields["z"] = yetAnotherVal;
updateItem.Editing.EndEdit();

When I run this bulk import code as admin, I want my items to go through workflow so that a human is involved in approving or rejecting the items that got imported – but that doesn’t happen, because admin users ignore workflow.

Is there a way to programmatically send the items I modify to workflow? There's an Item.State.GetWorkflowState(), but there doesn’t seem to be a corresponding SetWorkflowState().


Solution

  • Publishing content without approval of content approver is not a good approach.

    I am not 100% sure about this, as I haven't tried this.

    Here is code that I found:

    //add to workflow if requried and place it in start state and then execute the final stage
    Sitecore.Workflows.IWorkflow workflow = master.WorkflowProvider.GetWorkflow(newItem);
    workflow.Start(newItem);
    workflow.Execute(Config.AutoPublishCommandID, newItem, "auto approved", false);
    

    For publishing:

    //publish to pre-defined targets and langugaes
    Database[] targetDBs = new Database[] {        Sitecore.Configuration.Factory.GetDatabase("web") };
    Language[] languages = new Language[] { LanguageManager.GetLanguage("en") };
    Sitecore.Publishing.PublishManager.PublishIncremental(master, targetDBs, languages);