天天看點

Create custom Task List and Forms in SharePoint 2010 with Visual Studio 2012

When customising SharePoint Workflow Task we usually need to create customised Task List and Forms. It's easy to create List in Visual Studio but even if the list is based on "Task" template it will not come up in the drop down list when creating Workflow Instance or coding use SPWorkflowAssociation.

This 3-step solution works with SP2010 and VS2012. It should also work for VS2010 and perhaps work with SP2013.

1. Create list. Just create a list in VS. You may use customised content type but List Template type must be 107 otherwise it cannot be used for Workflow Task purpose. If the template and instance are not 107 you have to change it.

List Template

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- Do not change the value of the Name attribute below. If it does not match the folder name of the List project item, an error will occur when the project is run. -->
    <ListTemplate
        Name="List1"
        Type="107"
        BaseType="0"
        OnQuickLaunch="TRUE"
        SecurityBits="11"
        Sequence="360"
        DisplayName="List1"
        Description="My List Definition"
        Image="/_layouts/images/ittask.png"/>
</Elements>
           

List Instance

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="List1"
                OnQuickLaunch="TRUE"
                TemplateType="107"
                Url="Lists/List1"
                Description="My List Instance">
  </ListInstance>
</Elements>
           

2. FeatureReceiver to run after activating Workflow feature. This will associate workflow instance with the task list you have created in the 1st step.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPWorkflowTemplate wfTemplate = web.WorkflowTemplates[new Guid("your-workflow-template-guid")];
                    SPList historyList = web.Lists["Workflow History"]; //use default history
                    SPList taskList = web.Lists["List1"];
                    SPList listInstance = web.Lists["Documents"];

                    SPWorkflowAssociation wfAssociation = SPWorkflowAssociation.CreateListAssociation(
                        wfTemplate, wfTemplate.Name, taskList, historyList);
                    // Set workflow parameters 
                    wfAssociation.AllowManual = true;
                    wfAssociation.AutoStartCreate = false;
                    wfAssociation.AutoStartChange = false;

                    listInstance.WorkflowAssociations.Add(wfAssociation);
                }
            }
        }
           

3. This is another FeatureReceiver which link customised forms to custom content type. Can be run at appropriate place. 

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPSite site = properties.Feature.Parent as SPSite)
    {
                using (SPWeb web = site.OpenWeb())
                {
                        SPContentType ct = web.ContentTypes[new SPContentTypeId("0x01080100... ...")];
                        ct.NewFormUrl = "_layouts/MyProject/TaskEditForm.aspx";
                        ct.EditFormUrl = "_layouts/MyProject/TaskEditForm.aspx";
                        ct.DisplayFormUrl = "_layouts/MyProject/TaskEditForm.aspx";
                        ct.Update(); 
                }
    }
}

           

繼續閱讀