14 November, 2012

Add summary task to task list programatically

Hello again, recently I've tried to add a summary task in a task list in an event receiver.
For my surprise that turned out to be more difficult then I initially expected. Nevertheless, the solution is pretty simple.

Check it out

In your event receiver apply the following code changing:

  • The name of the Task list
  •  The name of the Summary Task content type
  • URL of the Task list
  • Summary Task title

string TaskListName = "Tasks";
string SummaryTaskContentType = "Summary Task";
string TaskListUrl = "{site url}/Lists/Tasks";
string SummaryTaskTitle = "My Summary Task";

SPList myList = SPContext.Current.Web.Lists[TaskListName];
SPContentType type = myList.ContentTypes[SummaryTaskContentType];
SPListItem newItem = myList.Items.Add(TaskListUrl , SPFileSystemObjectType.Folder, SummaryTaskTitle );

newItem["ContentTypeId"] = type.Id;          

Pretty simple isn't it ;)

If you want to keep in touch, feel free to Subscribe to How I did it - Sharepoint Foundation 2010