Annoying issue with sitecore workflows and standard value inheritance
Asked Answered
A

2

10

Right now i'm having a problem with sitecore workflow. The issue is as follows:

my template standard value has set the Workflow and Default Workflow fields set. The workflow state is set to draft. However, everytime i create an item from this template it is not inheriting the workflow section fields, all of them are empty

if i manually set workflow and workflow state for items, it works as expected up to the final state. However, if a content author edits the item, a new version is created but only the workflow field value is kept, while the workflow state is again empty.

what i'm doing wrong? why is this happening? I'm using sitecore 6.5.0

Annadiane answered 20/6, 2012 at 19:43 Comment(0)
T
23

The __Standard Values of your template should only have the Default workflow field set; do not set the Workflow field on the __Standard Values item as that insinuates that the __Standard Values item is itself in the workflow! Similarly, do not set the State field on the __Standard Values item as that is saying that the __Standard Values item is in that state!

Your Workflow item has an Initial State field on it that defines the initial state an item will be in (assuming the item uses that workflow).

When you create an item, and that item has a __Standard Value with a workflow in the Default Workflow field, then that item will have its Workflow field set the same as the Default Workflow field of its __Standard Values. The state the item is in will be the Initial State as defined on said workflow.

Trafficator answered 20/6, 2012 at 19:52 Comment(1)
We are doing exactly this, and seeing something we can't figure out: creating an item from the UI, this works as you describe, but creating it from code (item.Add specifying the template that has its __Standard Values Default workflow set, where item is a content folder), doesn't set the created item in the workflow (Workflow field remains empty). We're stumped as to what could cause that, any idea?Sesquipedalian
E
3

When you create the item via the API you appear to have to specifically set the workflow and the state explicitly:

Item myItem = parentItem.Add("New item", myTemplate);
myItem.Editing.BeginEdit();
// set any other fields you want here
...
// Now explicitly set the workflow and state here on your new item
// (make sure you get the correct GUIDs by viewing raw values on your workflow and workflow state items)...
myItem[FieldIDs.Workflow] = "{212BCCCB-8197-45C0-81CC-621E0553C25F}";
myItem[FieldIDs.WorkflowState] = "{E8228D86-D780-4F34-B372-6A4D29B67AF9}";
myItem.Editing.EndEdit(); 

I'm assuming that when you do it through the UI there are some extra pipelines that get processed that do this for you when using Content Editor etc, which you bypass by using the API.

In fact, here is a relevant post on SDN: http://sdn.sitecore.net/Forum/ShowPost.aspx?PostID=45991

Evelineevelinn answered 5/10, 2012 at 10:14 Comment(3)
Don't use this way with hardcoded Guids, check this page out and look at jRobbins answer on how to set it properly. #13528931Laplante
@Martin You missed the point. The code was not intended to be production ready - it was purely for the purpose of demonstration. Agreed of course don't hardcode ID's etc in prod code! Fact remains that when you do it through the UI there are some extra pipelines that get processed that do this for you when using Content Editor etc. Hardly think it was worth the downvoteEvelineevelinn
regarding the "proper" way to set a workflow at #13528931 : I don't think the proposed overly complicated code with 4(!) nested if statements should be the "proper" way to do something. moreover doesn't seems to answer the question: how to assign a workflow and a random workflow state to an item. Instead it basically moves the item to the next workflow state if the next state happens to be the desired stateFrei

© 2022 - 2024 — McMap. All rights reserved.