I came across couple of questions from Internet about how to programmatically create a Gannt view on a custom SharePoint list. So, here is an answer to these questions.



1. First of all, create a custom list from a SharePoint UI.

2. On the menu of the newly created custom list, click Settings->Create Column.

3. Create a column for Start Date with "Date and Time" type with Date Only as a format (see the picture below). The column name doesn't need to be exactly same.

 



4. Create a column for End Date with "Date and Time" type with Date Olnly as a format.
5.
Create a column for PercentComplete with "Number" type. Set Min to 0 and Max to 100. Set number of decimal place to 0, and check the "Show as percentage (for example, 50%)" option. See the picture below.



Now, run the following code to create a Gannt view.


using System;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using(SPSite site = new SPSite("[site url]"))
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["CustomList"];
SPViewCollection views = list.Views;
System.Collections.Specialized.StringCollection viewFields =
new System.Collections.Specialized.StringCollection();
          viewFields.Add("Title");
          SPView view = views.Add("Gannt Test", 
viewFields, String.Empty, 100, true, false,
SPViewCollection.SPViewType.Gantt, false);
view.ViewData = @"<FieldRef Name=""StartDate"" Type=""GanttStartDate"" />
<FieldRef Name=""DueDate"" Type=""GanttEndDate"" />
<FieldRef Name=""Title"" Type=""GanttTitle"" />
<FieldRef Name=""PercentComplete""
Type=""GanttPercentComplete"" />";

view.Update();
}
}
}
}

The key point on the code is that you need to use GanttStartDate, GanntEndDate, GanntDueDate and GanntPercentComplete as types of FieldRefs for ViewData.