SharePoint Workflow Assocation Page base class

public enum AssociationType
{
List,
ContentType,
ContentTypeOnList,
}

public abstract class AssociationPageBase : LayoutsPageBase
{
protected string workflowAssociationName = string.Empty;
protected Guid workflowTemplateId;
protected SPWorkflowTemplate workflowTemplate;
protected bool workflowAssociationExists;
protected bool updateContentTypesOnLists;
protected AssociationType associationType;
protected Guid listId;
protected SPList list;
protected SPContentTypeId contentTypeId;
protected SPContentType contentType;
protected SPWorkflowAssociation workflowAssociation;
protected Guid workflowAssociationId;
protected bool newTaskListRequired = false;
protected string newTaskListName = string.Empty;
protected bool newHistoryListRequired = false;
protected string newHistoryListName = string.Empty;
protected SPList taskList;
protected SPList historyList;

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.workflowAssociationName = this.Request.Params["WorkflowName"];
this.workflowTemplateId = new Guid(this.Request.Params["WorkflowDefinition"]);
this.workflowTemplate = this.Web.WorkflowTemplates[this.workflowTemplateId];
if (!string.IsNullOrEmpty(this.Request.Params["GuidAssoc"]))
{
this.workflowAssociationExists = true;
this.workflowAssociationId = new Guid(this.Request.Params["GuidAssoc"]);
}
this.updateContentTypesOnLists = (this.Request.Params["UpdateLists"] == "TRUE");
if (!string.IsNullOrEmpty(this.Request.Params["List"]))
{
this.listId = new Guid(this.Request.Params["List"]);
this.list = Web.Lists[this.listId];
}
if (string.IsNullOrEmpty(this.Request.Params["ctype"]))
{
this.associationType = AssociationType.List;
if (this.workflowAssociationExists)
{
this.workflowAssociation = this.list.WorkflowAssociations[this.workflowAssociationId];
}
}
else
{
this.contentTypeId = new SPContentTypeId(this.Request.Params["ctype"]);
if (this.list == null)
{
this.associationType = AssociationType.ContentType;
this.contentType = this.Web.AvailableContentTypes[this.contentTypeId];
}
else
{
this.associationType = AssociationType.ContentTypeOnList;
this.contentType = this.list.ContentTypes[this.contentTypeId];
}
if (this.workflowAssociationExists)
{
this.workflowAssociation = this.contentType.WorkflowAssociations[this.workflowAssociationId];
}
}
string taskListId = this.Request.Params["TaskList"];
if (taskListId[0] == 'z')
{
this.newTaskListRequired = true;
this.newTaskListName = taskListId.Substring(1);
}
else
{
if (this.associationType == AssociationType.ContentType)
{
try
{
this.taskList = this.Web.Lists[taskListId];
}
catch (Exception)
{
this.newTaskListRequired = true;
this.newTaskListName = taskListId;
}
}
else
{
this.taskList = this.Web.Lists[new Guid(taskListId)];
}
}
string historyListId = this.Request.Params["HistoryList"];
if (historyListId[0] == 'z')
{
this.newHistoryListRequired = true;
this.newHistoryListName = historyListId.Substring(1);
}
else
{
if (this.associationType == AssociationType.ContentType)
{
try
{
this.historyList = this.Web.Lists[historyListId];
}
catch (Exception)
{
this.newHistoryListRequired = true;
this.newHistoryListName = historyListId;
}
}
else
{
this.historyList = this.Web.Lists[new Guid(historyListId)];
}
}
}

protected void Submit()
{
if (this.newTaskListRequired)
{
Guid taskListId = this.Web.Lists.Add(this.newTaskListName, this.newTaskListName, SPListTemplateType.Tasks);
this.taskList = this.Web.Lists[taskListId];
}
if (this.newHistoryListRequired)
{
Guid historyListId = this.Web.Lists.Add(this.newHistoryListName, this.newHistoryListName, SPListTemplateType.WorkflowHistory);
this.historyList = this.Web.Lists[historyListId];
}
switch (this.associationType)
{
case AssociationType.List:
if (this.workflowAssociationExists)
{
this.UpdateWorkflowAssociation();
this.list.UpdateWorkflowAssociation(this.workflowAssociation);
}
else
{
this.workflowAssociation = SPWorkflowAssociation.CreateListAssociation(
this.workflowTemplate, this.workflowAssociationName, this.taskList, this.historyList);
this.UpdateWorkflowAssociation();
this.list.AddWorkflowAssociation(this.workflowAssociation);
}
break;
case AssociationType.ContentType:
if (this.workflowAssociationExists)
{
this.UpdateWorkflowAssociation();
this.contentType.UpdateWorkflowAssociation(this.workflowAssociation);
if (this.updateContentTypesOnLists)
{
this.contentType.UpdateWorkflowAssociationsOnChildren(true, true, true);
}
}
else
{
this.workflowAssociation = SPWorkflowAssociation.CreateSiteContentTypeAssociation(
this.workflowTemplate,
this.workflowAssociationName,
this.taskList.Title,
this.historyList.Title);
this.UpdateWorkflowAssociation();
this.contentType.AddWorkflowAssociation(this.workflowAssociation);
if (this.updateContentTypesOnLists)
{
this.contentType.UpdateWorkflowAssociationsOnChildren(true, true, true);
}
}
break;
case AssociationType.ContentTypeOnList:
if (this.workflowAssociationExists)
{
this.UpdateWorkflowAssociation();
this.contentType.UpdateWorkflowAssociation(this.workflowAssociation);
}
else
{
this.workflowAssociation = SPWorkflowAssociation.CreateListContentTypeAssociation(
this.workflowTemplate, this.workflowAssociationName, this.taskList, this.historyList);
this.UpdateWorkflowAssociation();
this.contentType.UpdateWorkflowAssociation(this.workflowAssociation);
}
break;
}
SPUtility.Redirect(this.GetRedirectUrl(), SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
}

protected abstract string CollectAssociationData();

protected void Cancel()
{
SPUtility.Redirect(this.GetRedirectUrl(), SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
}

private void UpdateWorkflowAssociation()
{
this.workflowAssociation.Name = this.workflowAssociationName;
this.workflowAssociation.AutoStartCreate = (this.Request["AutoStartCreate"] == "ON");
this.workflowAssociation.AutoStartChange = (this.Request["AutoStartChange"] == "ON");
this.workflowAssociation.AllowManual = (this.Request["AllowManual"] == "ON");
this.workflowAssociation.AssociationData = this.CollectAssociationData();
if (this.workflowAssociation.TaskListTitle != this.taskList.Title)
{
this.workflowAssociation.SetTaskList(this.taskList);
}
if (this.workflowAssociation.HistoryListTitle != this.historyList.Title)
{
this.workflowAssociation.SetHistoryList(this.historyList);
}
}

private string GetRedirectUrl()
{
string redirectUrl = string.Empty;
switch (this.associationType)
{
case AssociationType.List:
redirectUrl = string.Format("WrkSetng.aspx?List={0}", this.listId);
break;
case AssociationType.ContentType:
redirectUrl = string.Format("WrkSetng.aspx?ctype={0}", this.contentTypeId);
break;
case AssociationType.ContentTypeOnList:
redirectUrl = string.Format("WrkSetng.aspx?List={0}&ctype={1}", this.listId, this.contentTypeId);
break;
}
return redirectUrl;
}
}

How to use:
1. Use this class as the base class of your custom workflow assocation page;
2. Use the protected field member to get the related list, workflow template, content type etc;
3. Override CollectAssociationData to collect data from web form and get your custom assocation data(xml format etc.);
4. Invoke Submit when you click "OK" to commit your change, invoke Cancel when you click "Cancel".
Hope to help you, any question please add comments.

0 comments: