Create Sharepoint List which has gantt view - programmatically
Asked Answered
A

1

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

Anatomy answered 15/10, 2009 at 13:53 Comment(0)
R
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

Reduced answered 15/10, 2009 at 14:17 Comment(7)
Thank you very much for your reply...Will this show the list automatically as a gant chart? Thank you so much!Anatomy
I meant linking the gantt properties such as startdate, enddate ,etc...please?Anatomy
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();Anatomy
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!Anatomy
WORKS AMAZINGLY GREAT! THANK YOU SO MUCH!!! YOU'VE BEEN VERY HELPFUL THANK YOU!!!Anatomy
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!Anatomy
i've created the new question as requested - can you please help? https://mcmap.net/q/911702/-update-sharepoint-list-itemAnatomy

© 2022 - 2024 — McMap. All rights reserved.