views:

1123

answers:

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 cololumns (that would generate the Gantt chart) to the List 3. And finally i would like to add values/data to the coloums created via this code too...

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

any help would be much appreciated please

thank you so much

+1  A: 

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
Thank you very much for your reply...Will this show the list automatically as a gant chart?Thank you so much!
Mo
I meant linking the gantt properties such as startdate, enddate ,etc...please?
Mo
yes, all standard properties will be there
Rubens Farias
Sorry to be a pain but when i add the other properties it does not worki 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();
Mo
see updated code
Rubens Farias
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!
Mo
see updated code again
Rubens Farias
WORKS AMAZINGLY GREAT! THANK YOU SO MUCH!!! YOU'VE BEEN VERY HELPFUL THANK YOU!!!
Mo
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? pleasethank you!
Mo
updated again; please check it out
Rubens Farias
THANK YOU MUCH APPRECIATED!
Mo
i've created the new question as requested - can you please help?http://stackoverflow.com/questions/1578361/update-sharepoint-list-item
Mo