Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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();
share|improve this answer
    
I have tried that, but it doesn't work. I also see, that if i try to fill any other field after the first Update(), the value is not changed, so maybe the problem is with the steps i do to create this item. I used this article link but since its for 2007, maybe something is out of date –  matt137 Nov 21 '11 at 12:05
    
ok, a litte disclaimer to teh comment above: I can modify the 'default' fields, like Description, but I have also added 2 extra fields (currency) and i can't modify those. Still, Name column is not being modyfied (but the Name property has the good value while debugging) –  matt137 Nov 21 '11 at 12:41
    
this works indeed, I have not tried the overloaded Add() and it looks like I should have had.. thanks a lot! –  matt137 Nov 21 '11 at 13:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.