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.
Labels: MOSS2007, SharePoint, sharepoint workflow
SharePoint Alert provides Email-alert function but cannot fully meet users’ demands. SharePoint Alert Boost greatly extends default SharePoint Alert function and make the application of Alert more convenient. The new features are as follows:
1. Set alert conditions;
2. Customize email content;
3. Insert the columns into the alerts
4. Send email to non-SharePoint users or groups
5. Send email to the task-related users
6. Send item attachment as email attachment
Now, let’s turn to the following sample:
Director Henry assigned Tom a task for which the due date is 15th July. Meanwhile, a copy of description document is added to task item. When the task is completed, Henry would have a check. Email notification will be sent to task-related persons once meeting the following conditions:
Firstly, as long as Henry creates the task item, Email will be sent to Tom;
Secondly, send the item attachment as the email attachment
Thirdly, on the due date, a piece of Email will report to Henry;
Fourthly, Henry will receive a mail when the task is completed;
SharePoint Alert cannot achieve above requests, while SharePoint Alert Boost does work.
If the user wants to achieve the request as above referred, he can configure settings in sharepoint alert boost.
Add the requrired columns, such as director,assigned to, status, and so on. Then enter sharepoint alert workflow setting page, and create a new Alert Part. In the following steps, you can configure the main settings according to above request in releated area. For instance, customize Email subject and content, decide BCC to and Sent to, and set alert condition.
SharePoint Alert Reminder is now available for 30 days free trial, you can download it from:
http://www.sharepointboost.com/alertreminderboost.html