1

I am new to Sharepoint therefore don't know much - any help would be highly appreciated.

Basically I want to programatically (in the same project):

  1. Create a List and make it a Gantt View
  2. Add add appropriate columns (that would generate the Gantt chart) to the list
  3. And finally I would like to add values/data to the columns created via this code

If there is a sample code or any tutorial...please

Any help would be much appreciated please

Thank you so much

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
K-M
  • 660
  • 3
  • 13
  • 27

1 Answers1

1

Try this:

using (SPSite site = new SPSite("http://yoursite/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        Guid id = web.Lists.Add("listname", "descr", // 1
                                 SPListTemplateType.GanttTasks);

        SPList list = web.Lists[id]; // 2
        list.Fields.Add("display name", SPFieldType.Text, false);
        list.Update();

        // You should use "InternalName" to update your field values
        foreach (SPField field in list.Fields)
        {
            Console.WriteLine("{0}\t{1}", field.InternalName, field.Title);
        }

        SPListItem item = list.Items.Add(); // 3
        item["display name"] = "my value";
        item["PercentComplete"] = 1; // 100%
        item["StartDate"] = DateTime.Now;
        item["DueDate"] = new DateTime(2009, 12, 31);
        item.Update();

        Guid itemId = item.UniqueId;
        SPListItem itemUpdate = web.Lists["listname"].Items[itemId];
        itemUpdate["PercentComplete"] = .45; // 45%
        itemUpdate.Update();
    }
}

HTH

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • Thank you very much for your reply...Will this show the list automatically as a gant chart? Thank you so much! – K-M Oct 15 '09 at 14:24
  • I meant linking the gantt properties such as startdate, enddate ,etc...please? – K-M Oct 15 '09 at 14:25
  • Sorry to be a pain but when i add the other properties it does not work i want to add the startdate, end date and other details as shown below list.Update(); SPListItem item = list.Items.Add(); // 3 item["Title"] = "TaskTest"; item["Task Status"] = "In Progress"; item["% Complete"] = 59; item["Start Date"] = "10/10/2009"; item["Due Date"] = "25/10/102009"; item.Update(); – K-M Oct 15 '09 at 14:49
  • superb! this works perfect - last one more favour please...i really appreciate... item["Start Date"] = 10/10/2009; is not working for me.....how can i add the date do i have to use speech marks plsss?? thank you so much....god bless! – K-M Oct 15 '09 at 15:15
  • WORKS AMAZINGLY GREAT! THANK YOU SO MUCH!!! YOU'VE BEEN VERY HELPFUL THANK YOU!!! – K-M Oct 15 '09 at 15:43
  • One last question....once the list has been created and i have added one record...if i want to add more records will i use Getlist instead of the Add? And also if i want to update the same values again? e.g you've set the % to 100 and for e.g. i wish to change it to 45? please thank you! – K-M Oct 15 '09 at 16:36
  • i've created the new question as requested - can you please help? http://stackoverflow.com/questions/1578361/update-sharepoint-list-item – K-M Oct 16 '09 at 14:37