Add custom Workflow to a list

I have putted this in a feature receiver event.
The code is actually very straight forword.

 

[Guid(“9ea3c6f6-f6f2-45e2-9cee-2e95e3019a34”)]
public class MUACChangeEventReceiver : SPFeatureReceiver
{

SPList historyList = null;
SPList taskList = null;

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb;
SPList list = web.Lists[“Project site creation”];

TryGetHistoryList(web);
TryGetTaskList(web);

//Get workflow by name
//SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName(Resources.Workflow.WorkflowDisplayName, CultureInfo.CurrentCulture);

Guid workflowTemplateGuid = Resources.Workflow.WorkflowId;
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates[workflowTemplateGuid];

web.AllowUnsafeUpdates = true;

try
{
// Create workflow association
SPWorkflowAssociation workflowAssociation = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, Resources.Workflow.WorkflowDisplayName, taskList, historyList);

// Set workflow parameters
workflowAssociation.AllowManual = false;
workflowAssociation.AutoStartCreate = true;
workflowAssociation.AutoStartChange = false;

// Add workflow association to my list
list.WorkflowAssociations.Add(workflowAssociation);

// Enable workflow
workflowAssociation.Enabled = true;
}
finally
{
web.AllowUnsafeUpdates = false;
}
}

private void TryGetHistoryList(SPWeb web)
{
try
{
historyList = web.Lists[“Workflow History”];
}
catch (ArgumentException exc)
{
// Create workflow history list
Guid listGuid = web.Lists.Add(“Workflow History”, “”, SPListTemplateType.WorkflowHistory);
historyList = web.Lists[listGuid];
historyList.Hidden = true;
historyList.Update();
}
}
private void TryGetTaskList(SPWeb web)
{
try
{
taskList = web.Lists[“Workflow Tasks”];
}
catch (ArgumentException exc)
{
// Create workflow tasks list
Guid listGuid = web.Lists.Add(“Workflow Tasks”, “”, SPListTemplateType.Tasks);
taskList = web.Lists[listGuid];
taskList.Hidden = true;
taskList.Update();
}
}
}

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s