I'm implementing workflow for page. I want to associate workflow definition to structure group to start workflow, but I have a problem about security rights.
There are following users.
- User A : Administrator who have 'Workflow Management' right.
- User B : Editor who does not have 'Workflow Management' right.
On my implementation, User B creates structure group. But User B cannot asscociate workflow definition to structure group because he doesn't have 'Workflow Management' right. I made event system that associate workflow definition to structure group when it's saved, but same problem occured(Tridion says 'You do not have permission to perform this action'). My event system code looks like this.I try to run my program by "administrator" user.
public void Subscribe()
{
EventSystem.Subscribe<StructureGroup, SaveEventArgs>(SetWorkflowToSG, EventPhases.Initiated);
}
private void SetWorkflowToSG(StructureGroup sg, SaveEventArgs args, EventPhases phase)
{
Session session = new Session("administrator");
ProcessDefinition pd = (ProcessDefinition)session.GetObject("tcm:5-21-131074");
sg.PageProcess = pd;
}
How can I achive my requirement that User B creates structure group and associtate workflow definition to it?
Following answers, I modified source code. It works fine.
public void Subscribe()
{
Subscriptions.Add(EventSystem.Subscribe<StructureGroup, SaveEventArgs>(SetWorkflowToSG, EventPhases.TransactionCommitted));
}
private void SetWorkflowToSG(StructureGroup sg, SaveEventArgs args, EventPhases phase)
{
//To avoid infinite loop, check value in Session.ContextData
if(!sg.Session.ContextData.ContainsKey("isTriggeredByEvent")){
Session newSession = new Session("administrator");
StructureGroup newSg = (StructureGroup)newSession.GetObject(sg.Id);
ProcessDefinition pd = (ProcessDefinition)newSession.GetObject("tcm:5-21-131074");
newSg.PageProcess = pd;
//Add value to Session.ContextData
newSg.Session.ContextData.Add(new KeyValuePair<string, object>("isTriggeredByEvent", true));
newSg.Save();
newSession.Dispose();
}
}
Regards,