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(); 
                }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值