Pages

17 November, 2012

Get parent Summary Task and children tasks

Good evening people, this week I've faced some nice challenge in Sharepoint 2010. Working with a Task, I needed to get the parent Summary Task of Task after it was added, changed or deleted, in order to update that same Summary Task with information about its children tasks.

It took me a few hours to solve this problem, yet solved it with a simple solution. Keep reading :)

1. Get the parent Summary Task after adding, updating or deleting Task event
Ok so the first step is to get the parent Summary Task, if a task is a child task of a Summary Task,

SPFolder folder = properties.ListItem.Web.GetFile(properties.ListItem.Url).ParentFolder;
SPListItem summaryTask = folder.Item;

Ok so now summaryTask is your parent Summary Task item, ready to be updated with whatever needed.

2. Get all the children Task of the previous Summary Task
So all items have an hidden field called FileDirRef, which points to the parent folder URL. In our case, our parent Summary Task. The idea is to execute a recursive query to the task list, filtering the field FileDirRef with the value of our added, uptaded or deleted Task FileDirRef.


SPQuery query = new SPQuery();

query.Query = string.Concat("<Where><Eq>",
  "<FieldRef Name='FileDirRef'/>",
  "<Value Type='Text'>" + properties.ListItem["FileDirRef"].ToString() + "</Value>",
"</Eq></Where>");

query.ViewAttributes = "Scope=\"Recursive\"";

foreach (SPListItem auxItem in properties.List.GetItems(query))
{
  //appply your code here
}

PS: One note, this will show only the sub Task items, if in your Summary Task you have sub Summary Tasks, you can access them through folder.SubFolders collection.


Hope it helps, took me a few hours to make this 10 code lines :)

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

No comments:

Post a Comment