I have configured a workflow, starts with "Draft" state as usual. And I have configured the workflow for standard values of the template. It works perfect in Content editor. (When I create an item using the content editor, once I create the item, Workflow gets assigned to the item and it's state becomes "Draft".)
But when I create an item programmatically under the above template it does not assign the workflow. What should I do to assign workflow? Please share any code samples if you have.
Thanks.
Sitecore.Data.Items.TemplateItem template = this.MasterDatabase.GetItem("/sitecore/templates/user defined/sample types");
Sitecore.Data.Items.Item parent = this.MasterDatabase.GetItem(parentId);
Sitecore.Data.Items.Item newItem;
// Create new item and retrieve it
newItem = template.CreateItemFrom("sampleName", parent);
newItem.Editing.BeginEdit();
newItem.Name = StringFormatter.CreateItemNameFromID(this.newItem);
newItem.Fields["Title"].Value = "Sample Title"
newItem.Editing.EndEdit();
I looked all over for a good answer to this. I was creating a new item and wanted to start its workflow. jRobbins's answer didn't work for me because it throws an exception if the current workflow state of the item is null and it didn't offer a good way of setting the initial workflow state.
The following worked for me:
This sets the workflow of my new item plus sets its workflow state to the initial state.
After making sure I'd set a default workflow on the standard values, the following combination, with thanks to posters above, worked perfectly:
Solved the issue with Standard Fields,
I would not use Dhanuka777's answer as it does not utilise Sitecore's Workflow State Commands and the useful functionality that comes with it e.g. email generation. It also requires hardcoding Guids.
Benefits of the below code: