Question

Hello

I have a Task List, and I want to add items to it programaticaly in C#. I have a problem with adding a summary task. Now I figured out how to add such item:

 var myList = SPContext.Current.Web.Lists["MyList"];
    SPContentType type = myList.ContentTypes["Summary Task"];
    SPListItem newItem= myList.Folders.Add();
    newItem["ContentTypeId"] = type.Id;
    newItem.Update();
    newItem["Title"] = "MyTitle";
    newItem.Update();
    myList.Update();

The problem is, Title column contains something like "79_.000" (the number increments for newly added element, guess its the id) instead of "MyTitle". When I skip the newItem["ContentTypeId"] = type.Id; line (and update) tthe name is ok, but the item is not recognized as a summary task. I see that the DisplayName property is the wrong one, but Name is ok. Unfortunately DisplayName is read only, so I can't modify it (but maybe this would not be the best approach)

I'm using SP 2010 foundation. Any help would be apprectated

Was it helpful?

Solution

EDIT

Just checked it out on my environment, and you need to create the item as a folder. Summary Tasks is a folder within a task list.

The code for this is the following:

var myList = web.Lists["Tasks"];
SPContentType type = myList.ContentTypes["Summary Task"];
// URL to your task list, Item type (Folder), Folder name (Your title)
SPListItem newItem = myList.Items.Add("/Lists/Tasks", SPFileSystemObjectType.Folder, "MyTitle");
newItem["ContentTypeId"] = type.Id;
// Change description
newItem["Body"] = "Description";
newItem.Update();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top