How to create a role task by code in Episerver

After failing duplicating the ParallelApproval Workflow I looked into creating a task by code in the project instead. Didn't find much documentation online so here's my initial Proof of Concept to fill the gap.

This will create a task from the current user and assign it to all users with the WebAdmins role. An e-mail will go out to all users in the role and when one of them change the status an e-mail will go back to the creator.

using EPiServer.Personalization;
Task currentTask = new Task();
currentTask.Subject = "Test";
currentTask.Description = "Some more text";
currentTask.AssignedTo = "WebAdmins";
currentTask.AssignedIsRole = true;
currentTask.DueDate = DateTime.Now.AddDays(1);
// Would be better to somehow fetch PlugInID
// by it's name "EPiServer.UI.Util.PlugIns.ViewTask"
// instead of this:
currentTask.PlugInID = 144;

Tested using Episerver CMS 5 R2 SP2.

Update: See code from Mårten Berg on how to get the PlugInID by name below.

PlugInDescriptor pid = PlugInDescriptor.Load("EPiServer.UI.Util.PlugIns.ViewTask", "EPiServer.UI");
currentTask.PlugInID = pid.ID;

Published and tagged with these categories: Episerver, ASP.NET, Development